35.QT蝴蝶飞舞

  • fly.h
     1 #ifndef FLY_H
     2 #define FLY_H
     3 #include <QObject>
     4 #include <QPainter>
     5 #include <QGraphicsScene>
     6 #include <QGraphicsView>
     7 #include <QGraphicsItem>
     8 #include <QPixmap>
     9 #include <math.h>
    10 #include <QTime>
    11 #include <QEvent>
    12 
    13 //图元对象
    14 class fly : public QObject,public QGraphicsItem
    15 {
    16     Q_OBJECT
    17 public:
    18     explicit fly(QObject *parent = nullptr);
    19 
    20     //区域
    21     QRectF boundingRect() const;
    22     //时间定时器
    23     void timerEvent(QTimerEvent *event);
    24 signals:
    25 
    26 public slots:
    27 
    28 protected:
    29     //绘制
    30     void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget);
    31 
    32 public:
    33     bool up;
    34     QPixmap pix_up;
    35     QPixmap pix_down;
    36 
    37     qreal angle;
    38 
    39 };
    40 
    41 #endif // FLY_H
  • fly.cpp
     1 #include "fly.h"
     2 
     3 fly::fly(QObject *parent) : QObject(parent)
     4 {
     5     //载入
     6     up = true;
     7     pix_up.load("up.png");
     8     pix_down.load("down.png");
     9 
    10     startTimer(30);
    11 }
    12 
    13 //图元的边界区域
    14 QRectF fly::boundingRect() const
    15 {
    16     qreal angletemp = 2;
    17     return QRectF(-pix_up.width()/2-angletemp,
    18                   -pix_up.height()/2-angletemp,
    19                   pix_up.width()+angletemp*2,
    20                   pix_up.height()+angletemp*2);
    21 }
    22 
    23 //定时器函数
    24 void fly::timerEvent(QTimerEvent *event)
    25 {
    26     //边界控制
    27     qreal edgex = scene()->sceneRect().right()+boundingRect().width()/2;
    28     qreal edgetop=scene()->sceneRect().top()+boundingRect().height()/2;
    29     qreal edgebottom = scene()->sceneRect().bottom()+boundingRect().height()/2;
    30 
    31     if(pos().x()>=edgex)
    32         setPos(scene()->sceneRect().left(),pos().y());
    33     if(pos().y()<=edgetop)
    34         setPos(pos().x(),scene()->sceneRect().bottom());
    35     if(pos().y()>=edgebottom)
    36         setPos(pos().x(),scene()->sceneRect().top());
    37 
    38 #define PI 3.14
    39     angle += (qrand() % 10)/20.0;
    40     qreal dx = fabs(sin(angle*PI)*10);
    41     qreal dy = (qrand()%20)-10.0;
    42 
    43     //设置位置,转换坐标
    44     setPos(mapToParent(dx,dy));
    45 }
    46 
    47 void fly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    48 {
    49     if(up)
    50     {
    51         painter->drawPixmap(boundingRect().topLeft(),pix_up);
    52         up = !up;
    53     }
    54     else
    55     {
    56         painter->drawPixmap(boundingRect().topLeft(),pix_down);
    57         up = !up;
    58     }
    59 }
  • main.cpp
     1 #include "dialog.h"
     2 #include <QApplication>
     3 #include <QGraphicsScene>
     4 #include <QGraphicsView>
     5 #include "fly.h"
     6 
     7 int main(int argc, char *argv[])
     8 {
     9     QApplication a(argc, argv);
    10 
    11     //创建图像场景
    12     QGraphicsScene *scene = new QGraphicsScene;
    13     //设置区域
    14     scene->setSceneRect(-300,-200,400,500);
    15 
    16     fly *f = new fly;
    17     f->setPos(-100,0);
    18     scene->addItem(f);
    19 
    20     QGraphicsView *view = new QGraphicsView;
    21     view->setScene(scene);
    22     view->resize(800,600);
    23     view->show();
    24 
    25     return a.exec();
    26 }
原文地址:https://www.cnblogs.com/xiaochi/p/8795480.html