QGraphics框架基本使用方式

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值