QT QGraphicsItem飞舞的蝴蝶
来源:互联网 发布:淘宝生意参谋多少钱 编辑:程序博客网 时间:2024/04/27 13:44
一效果
二源代码
1butterfly.h
#ifndef BUTTERFLY_H#define BUTTERFLY_H#include<QObject>#include<QGraphicsItem>#include<QPainter>#include<QGraphicsScene>#include<QGraphicsView>#include<QRectF>#include<QWidget>#include<QDebug>#include<QPixmap>class ButterFly:public QObject,public QGraphicsItem{ Q_OBJECTpublic: 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
2mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include<butterfly.h>class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); ~MainWindow();};#endif // MAINWINDOW_H
3butterfly.cpp
#include "butterfly.h"#include <math.h>const static double PI=3.1416;ButterFly::ButterFly(QObject *parent) : QObject(parent){ up = true; if(pix_up.load("up.png")) qDebug("1"); if(pix_down.load("down.png"))qDebug("2"); startTimer(300); qDebug("butterFly");}QRectF ButterFly::boundingRect()const{ qreal adjust = 2; return QRectF(-pix_up.width()/2-adjust,-pix_up.height()-adjust,pix_up.width()+adjust,pix_up.height()+adjust);}void ButterFly::paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget){ if(up) { painter->drawPixmap(this->boundingRect().topLeft(),pix_up); up = ! up; } else { painter->drawPixmap(this->boundingRect().topLeft(),pix_down); up = ! up; } qDebug("paint");}void ButterFly::timerEvent(QTimerEvent *){ qreal edge_right = scene()->sceneRect().right()+boundingRect().width()/2; qreal edge_top = scene()->sceneRect().top()+boundingRect().height()/2; qreal edge_bottom = scene()->sceneRect().bottom()+boundingRect().height()/2; if(pos().x()>=edge_right) setPos(scene()->sceneRect().left(),pos().y()); if(pos().y()>=edge_bottom) setPos(scene()->sceneRect().bottom(),pos().y()); if(pos().x()<=edge_top) setPos(scene()->sceneRect().top(),pos().y()); angle +=(qrand()%10)/20.0; qreal dx=fabs(sin(angle*PI))*10.0; qreal dy=(qrand()%20)-10; setPos(mapToParent(dx,dy)); update(); qDebug("timerEvent");}
4main.cpp
#include "mainwindow.h"#include <QApplication>#include "butterfly.h"int main(int argc, char *argv[]){ QApplication a(argc, argv);// MainWindow w;// w.show(); 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();}
5mainwindow.cpp
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){}MainWindow::~MainWindow(){}
三感悟
1在butterfly.c中重载图元类ButterFly
2在其构造函数中加载飞舞的蝴蝶的两张姿态图片,并初始化定时器
ButterFly::ButterFly(QObject *parent) : QObject(parent)
{
up = true;
if(pix_up.load("up.png")) qDebug("1");
if(pix_down.load("down.png"))qDebug("2");
startTimer(300);
qDebug("butterFly");
}3飞舞的蝴蝶两种姿态变换通过paint()函数实现
void ButterFly::paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget)
{
if(up)
{
painter->drawPixmap(this->boundingRect().topLeft(),pix_up);
up = ! up;
}
else
{
painter->drawPixmap(this->boundingRect().topLeft(),pix_down);
up = ! up;
}
qDebug("paint");
}4通过定时器函数实现蝴蝶的飞舞
void ButterFly::timerEvent(QTimerEvent *)
{
qreal edge_right = scene()->sceneRect().right()+boundingRect().width()/2;
qreal edge_top = scene()->sceneRect().top()+boundingRect().height()/2;
qreal edge_bottom = scene()->sceneRect().bottom()+boundingRect().height()/2;
if(pos().x()>=edge_right)
setPos(scene()->sceneRect().left(),pos().y());
if(pos().y()>=edge_bottom)
setPos(scene()->sceneRect().bottom(),pos().y());
if(pos().x()<=edge_top)
setPos(scene()->sceneRect().top(),pos().y());
angle +=(qrand()%10)/20.0;
qreal dx=fabs(sin(angle*PI))*10.0;
qreal dy=(qrand()%20)-10;
setPos(mapToParent(dx,dy));
update();
qDebug("timerEvent");
}
阅读全文
0 0
- QT QGraphicsItem飞舞的蝴蝶
- Qt 飞舞的蝴蝶
- Qt 飞舞的蝴蝶
- Qt 动画飞舞的蝴蝶源码
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- 蝴蝶飞舞
- 补间动画--实现飞舞的蝴蝶
- 【Pyqt4 实例13】飞舞的蝴蝶
- 实现蝴蝶翩翩飞舞的效果
- QGraphic view实例:利用QGraphicsItem与定时器实现动画效果:蝴蝶飞舞
- 【Qt5开发及实例】20、实现一个飞舞的蝴蝶
- Qt中QGraphicsItem/View/Scene的哲学
- Android中的动画详解系列【2】——飞舞的蝴蝶
- Android中的动画详解系列【2】——飞舞的蝴蝶
- css3 的各种渐变总结
- HBase RowKey的设计原则
- angular-cli启动代理
- linux 去除icmp重定向
- 如何解决织梦DedeCms文章标题字数长度限制的方法教程
- QT QGraphicsItem飞舞的蝴蝶
- 一些有用的链接
- 18-常用对象API(集合框架-工具类-Arrays-asList方法)1 2 3 19-常用对象API(集合框架-工具类-Collection-toArray方法).1 2
- 小程序的生命周期及运行机制
- ORACLE数据库报ORA-09817、ORA-01157、ORA-01110错误解决方法
- Java中this和super的用法总结
- 自定义PopupWindow菜单
- JS学习
- 职业规划-数据库工程师(Database Engineer)