ButterFly

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

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 教程一:视频截图(Tutorial 01: Making Screencaps) 首先我们需要了解视频文件的一些基...
    90后的思维阅读 4,744评论 0 3
  • 1.项目经验 2.基础问题 3.指南认识 4.解决思路 ios开发三大块: 1.Oc基础 2.CocoaTouch...
    阳光的大男孩儿阅读 5,022评论 0 13
  • make menuconfig过程解析作者 codercjg 在 28 九月 2015, 5:27 下午 make...
    codercjg阅读 979评论 0 1
  • 上周去画室体验了一节彩铅课,感觉好棒!N年前收到的礼物终于可以派上用场了~~ 没有老师的指点,光影是放弃了,从来都...
    铃儿响金金阅读 188评论 0 1
  • 我得承认,最近的自己负能量很多,总是不停地向别人抱怨。由于自己每天会反思,在笔记上简要记录一天发生的事以及自己的情...
    清冷的蓝天天阅读 240评论 0 0