Qt5开发及实例学习之飞舞的蝴蝶

1、新建Qt GUI应用,基类QMainWindows,取消创建界面

2、项目名称上选择“添加新文件”--->"C++类"-->基类“QObject”,类名"Butterfly"

3、butterfly.h

#ifndef BUTTERFLY_H
#define BUTTERFLY_H

#include <QObject>
#include <QGraphicsItem>
#include <QPainter>
#include <QGraphicsScene>
#include <QGraphicsView>

class Butterfly : public QObject,public QGraphicsItem
{
    Q_OBJECT
public:
    explicit Butterfly(QObject *parent = nullptr);
    void timerEvent(QTimerEvent *);
    QRectF boundingRect() const;

signals:

public slots:

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

private:
    bool up;
    QPixmap pix_up;            //用于表示两幅蝴蝶的图片
    QPixmap pix_down;

    qreal angle;
};

#endif // BUTTERFLY_H

butterfly.cpp

#include "butterfly.h"
#include <math.h>

const static double PI=3.1416;

Butterfly::Butterfly(QObject *parent) :
    QObject(parent)
{
    up = true;
    pix_up.load("F:\\MyCode\\Butterfly\\up.png");
    pix_down.load("F:\\MyCode\\Butterfly\\down.png");

    startTimer(100);
}

QRectF Butterfly::boundingRect() const
{
   qreal adjust =2;
   return QRectF(-pix_up.width()/2-adjust,-pix_up.height()/2-adjust,pix_up.width()+adjust*2,pix_up.height()+adjust*2);
}

void Butterfly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//    if(up)
//    {
        painter->drawPixmap(boundingRect().topLeft(),pix_up);
        up=!up;
//    }
//    else
//    {
//        painter->drawPixmap(boundingRect().topLeft(),pix_down);
//        up=!up;
//    }
}

void Butterfly::timerEvent(QTimerEvent *)
{
    //边界控制
    qreal edgex=scene()->sceneRect().right()+boundingRect().width()/2;
    qreal edgetop=scene()->sceneRect().top()+boundingRect().height()/2;
    qreal edgebottom=scene()->sceneRect().bottom()+boundingRect(). height()/2;

    if(pos().x()>=edgex)
        setPos(scene()->sceneRect().left(),pos().y());
    if(pos().y()<=edgetop)
        setPos(pos().x(),scene()->sceneRect().bottom());
    if(pos().y()>=edgebottom)
        setPos(pos().x(),scene()->sceneRect().top());

    angle+=(qrand()%10)/20.0;
    qreal dx=fabs(sin(angle*PI)*10.0);
    qreal dy=(qrand()%20)-10.0;

    setPos(mapToParent(dx,dy));
}

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include "butterfly.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene *scene = new QGraphicsScene;
    scene->setSceneRect(QRectF(-200, -200, 400, 400));

    Butterfly *butterfly = new Butterfly;

    scene->addItem(butterfly);

    QGraphicsView *view = new QGraphicsView(scene);
    view->resize(400, 400);
    view->show();

    return a.exec();
}

4、分析:重新实现bounding&painter,在timerEvent中设置边界

 

 

转载于:https://my.oschina.net/u/3919756/blog/2052187

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值