|
5Qter豆
- #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 = 0);
- 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
复制代码- #include "butterfly.h"
- #include <math.h>
- const static double PI=3.1416;
- Butterfly::Butterfly(QObject *parent) :
- QObject(parent)
- {
- up = true;
- pix_up.load("D://CH7/CH701/Butterfly/up.jpg");
- pix_down.load("D://CH7/CH701/Butterfly/down.jpg");
- 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()%200)-10.0;
- setPos(mapToParent(dx,dy));
- }
复制代码- #include "mainwindow.h"
- #include <QApplication>
- #include "butterfly.h"
- #include <QGraphicsScene>
- 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;
- butterfly->setPos(-100,0);
- scene->addItem(butterfly);
-
- QGraphicsView *view = new QGraphicsView;
-
- view->setScene(scene);
- view->resize(400,400);
- view->show();
- return a.exec();
- }
复制代码 这是一个实现蝴蝶飞舞效果的程序
第一个是头文件 第二个是类函数定义
第三个是主函数
我想问大家
函数 C/C++ code?
1
2
3
4
5
6
7
8
9
10
11
12
13
| 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;
}
}
|
看它的定义 是实现图片变换的
可是在主函数里,找不到对它的调用
|
|