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 *);//声明定时器的timerEvent()函数
QRectF boundingRect() const; //该函数必须实现
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"
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); //时间间隔100毫秒
}
QRectF Butterfly::boundingRect() const //加载蝴蝶项目的限定范围,以其自身的坐标系为基础设定的
{
qreal adjust = 2;
return QRectF(-pix_up.width()/2-adjust,-pix_up.height()/2-adjust,
pix_up.width()+adjust2,pix_up.height()+2adjust);
}
//一下函数实现蝴蝶的飞舞效果
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 *)
{
// 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));//映射到场景的坐标
//update();
}
main函数
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);
//为每一个飞舞的湖底产生一个随机位置
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;
//为每一个飞舞的湖底产生一个随机位置
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();
}
网友评论