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");
}