ButterFly.h
ifndef BUTTERFLY_H
define BUTTERFLY_H
include <QtGui/QDialog>
include "ui_ButterFly.h"
include <QGraphicsScene>
include <QGraphicsView>
include <QGraphicsItem>
include <QObject>
class Butterfly : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
explicit Butterfly(QObject * parent = 0);
void timerEvent(QTimerEvent *);//聲明定時(shí)器的timerEvent()函數(shù)
QRectF boundingRect() const; //該函數(shù)必須實(shí)現(xiàn)
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
//重畫函數(shù)
private:
bool up; //用于實(shí)現(xiàn)蝴蝶的飛舞畫面
QPixmap pix_up; //蝴蝶圖案一
QPixmap pix_down; //蝴蝶
qreal angle;
};
endif // BUTTERFLY_H
ButterFly.cpp
include "ButterFly.h"
static const double PI = 3.14;
Butterfly::Butterfly(QObject * parent)
{
pix_up.load(":/images/Resources/butterfly1.png"); //圖片的加載
pix_down.load(":/images/Resources/butterfly2.png");
up = true;
startTimer(100); //時(shí)間間隔100毫秒
}
QRectF Butterfly::boundingRect() const //加載蝴蝶項(xiàng)目的限定范圍,以其自身的坐標(biāo)系為基礎(chǔ)設(shè)定的
{
qreal adjust = 2;
return QRectF(-pix_up.width()/2-adjust,-pix_up.height()/2-adjust,
pix_up.width()+adjust2,pix_up.height()+2adjust);
}
//一下函數(shù)實(shí)現(xiàn)蝴蝶的飛舞效果
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;
}
}
//判斷蝴蝶的運(yùn)動(dòng)范圍袍榆,并做相應(yīng)的處理相信根據(jù)函數(shù)名大家都知道啥意思
void Butterfly::timerEvent(QTimerEvent *)
{
// edge controll
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;
//flash = !flash;
setPos(mapToParent(dx,dy));//映射到場(chǎng)景的坐標(biāo)
//update();
}
main函數(shù)
include "ButterFly.h"
include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
if 0
QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(QRectF(-500,-500,500,500));
for(int i=0;i<10;i++){
Butterfly *butterfly = new Butterfly;
//butterfly->setPos(-100,0);
//為每一個(gè)飛舞的湖底產(chǎn)生一個(gè)隨機(jī)位置
butterfly->setPos((qrand()%int(scene->sceneRect().width()))-400,(qrand()%int(scene->sceneRect().height()))-300);
scene->addItem(butterfly);
}
QGraphicsView * view = new QGraphicsView;
view->setScene(scene);
//view->setMaximumSize(800,600);
//view->setMinimumSize(800,600);
view->resize(1000,1000);
view->show();
endif
QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(QRectF(-400,-300,800,600));
for(int i=0;i<100;i++){
Butterfly *butterfly = new Butterfly;
//為每一個(gè)飛舞的湖底產(chǎn)生一個(gè)隨機(jī)位置
butterfly->setPos((qrand()%int(scene->sceneRect().width()))-400,(qrand()%int(scene->sceneRect().height()))-300);
scene->addItem(butterfly);
}
QGraphicsView *view = new QGraphicsView;
view->setScene(scene);
view->setMaximumSize(800,600);
view->setMinimumSize(800,600);
view->show();
return a.exec();
}