QGraphics框架基本使用方式
tip:本文实现蝴蝶飞动的动画。
本文中的算法那些不要在意,关键理解QGraphics框架基本使用方式。
butterfly.h
#ifndef BUTTERFLY_H
#define BUTTERFLY_H
#include <QObject>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QPixmap>
#include <QRectF>
#include<QPainter>
#include<QTimerEvent>
#include<QDebug>
#include <math.h>
#define PI 3.1416
class Butterfly : public QObject,public QGraphicsItem
{
Q_OBJECT
public:
explicit Butterfly(QObject *parent = nullptr);
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
void timerEvent(QTimerEvent* ) override;
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
private:
bool up;
QPixmap pix_up;
QPixmap pix_down;
QTimer *time;
signals:
};
#endif // BUTTERFLY_H
butterfly.cpp
#include "butterfly.h"
Butterfly::Butterfly(QObject *parent) : QObject(parent)
{
up = true;
pix_up.load(":/new/prefix1/up.png"); //调用QPixmap的load()函数加载所用到的图片
pix_down.load(":/new/prefix1/down.png");
startTimer(100);
}
QRectF Butterfly::boundingRect() const
{
qreal adjust = 2;
if (up){
return QRectF(-pix_up.width()/2 - adjust, -pix_up.height()/2 - adjust, pix_up.width() + adjust * 2, pix_up.height() + adjust * 2);
}else{
return QRectF(-pix_down.width()/2 - adjust, -pix_down.height()/2 - adjust, pix_down.width() + adjust * 2, pix_down.height() + adjust * 2);
}
}
//paint 来自于 QGraphicsItem
//paintEvent 来自于 QWidget
//只有在这两个函数中,才可以使用painter绘画
void Butterfly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
if (up){
painter->drawPixmap(boundingRect().topLeft(),pix_up);
}else{
painter->drawPixmap(boundingRect().topLeft(),pix_down);
}
up = !up;
}
void Butterfly::timerEvent(QTimerEvent *)
{
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());
}
qreal dx = fabs(sin(( (qrand()%10)/20.0) * PI) * 10.0);
qreal dy = (qrand()%20) - 10.0;
setPos(mapToParent(dx, dy));
}
void Butterfly::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug()<<scene()->sceneRect();
qDebug()<<pos();
}
main.cpp
#include "butterfly.h"
#include <QApplication>
#include <QGraphicsView>
/*
QGraphics框架基本使用方式
1.自定义子对象类, 继承于 QGraphicsItem
2.实现对象的具体展示内容
3.使用QGraphicsItem 里的方法,控制自己的位置变换
4.创建QGraphicsScene对象,使用addItem添加自定义对象
5.QGraphicsScene 可以管理其子对象
6.创建QGraphicsView对象,使用setScene显示指定scene
7.QGraphicsView用于控制具体显示窗口信息
8.使用show()展示
*/
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(QRectF(-100, -100, 500, 500));
Butterfly *but = new Butterfly;
but->setPos(-50, 10);
scene->addItem(but);
QGraphicsView *view = new QGraphicsView;
view->setScene(scene);
view->resize(500,500);
view->show();
return a.exec();
}