QT QGraphicsItem飞舞的蝴蝶

一效果


二源代码

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_OBJECT
public:
    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_OBJECT

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值