ButterFly

作者: henry_bin | 来源:发表于2017-09-11 23:30 被阅读0次
butterfly.png

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();

}

相关文章

  • ButterFly

    ButterFly.h ifndef BUTTERFLY_H define BUTTERFLY_H include...

  • Struggle to Get out of Everythin

    Here is a story:A man sees a butterfly. The butterfly tri...

  • Butterfly

    星期五我接到他的死讯,到现在已经过去一周的时间了,这几天里我无所事事。天气很热,没想到立秋之后天气还这么热。临近中...

  • Into a butterfly

    The butterfly chrysalis in the game will not be the time ...

  • A butterfly

    You are a little butterfly Joyfully flys into my heart Wi...

  • Butterfly

    My time to leave for North Carolina was soon approaching....

  • Butterfly

    悬浮的空气 幽灵般飞舞的蝴蝶 二次元空间下从山的这头沉浸到山下还未烧尽的天涯海角 海天黎明 带走的只是海...

  • butterfly

    当初听着或者唱着匆匆那年的时候,一直对"我们要互相亏欠"这句词耿耿于怀; 即便是亏欠我的人不幸应了我对ta的怨怼而...

  • B

    Butterfly

  • Beast Butterfly

    Beast Butterfly

网友评论

      本文标题:ButterFly

      本文链接:https://www.haomeiwen.com/subject/oksqsxtx.html