Qt基于QGraphicsObject自定义图元并实现简单的动画

Qt基于QGraphicsObject自定义图元并实现简单的动画

Qt 图形的绘制 可以是QPainter方法直接绘制,另一种方法就是通过继承QGraphicsItem类来定制自己的图元类,这样就可以根据自己的需求来制作它的形状、颜色、属性以及交互等。但是这样的图元会存在一个缺点,那就是不能使用Qt的信号/槽机制,也不能使用Qt属性系统,也不能使用Qt的动画框架实现动画。

在Qt已经考虑到了这些问题,QGraphicsObject应运而生。QGraphicsObject同时继承于QObject和QGraphicsItem。这样自定义图元既可以获得QGraphicsItem中写好的图元特性又能使用信号/槽机制方便信息传递,还可以使用Qt的动画框架。

动画框架的好处在于,在单线程的UI设计中,可以实现不同的图元的各自的动画效果。

举例;

实现这样的效果:在场景中添加自定义图元,利用QQPropertyAnimation 动画框架实现动画。

过程:

1、新建一个的项目,命名可以自定义 如graphicObjectDemo。
2、添加类tagObject :
在这里插入图片描述
注意:如果该类要使用信号机制,记得一定要勾选include QObject、Add Q_OBJECT 。

改写 tagObject.cpp

#include "tagobject.h"
#include <QPainter>

tagObject::tagObject()
{
    brushColor = Qt::lightGray;
    setFlag(QGraphicsItem::ItemIsFocusable);
    setFlag(QGraphicsItem::ItemIsMovable);
}



QRectF tagObject::boundingRect() const
{
    qreal adjust = 0.5;
    return QRectF(-25 - adjust, -25 - adjust,
                  50 + adjust, 50 + adjust);
}

void tagObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                   QWidget *widget)
{

    painter->setBrush(brushColor);
    // 各个点的坐标
    static const QPointF points[3] = {QPointF(-8, 8), QPointF(8, 8), QPointF(0,-8)};
    painter->drawPolygon(points, 3);

}

tagOBject.h

#ifndef TAGOBJECT_H
#define TAGOBJECT_H

#include <QObject>
#include<QGraphicsObject>

class tagObject:public QGraphicsObject
{
    Q_OBJECT
public:
    tagObject();

    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                  QWidget *widget);
    void setColor(const QColor &color) { brushColor = color; }

    QColor brushColor;

};


改写mainwindows.cpp 函数,主要是完成如下几个任务创建场景,创建视图,添加图元,加载动画。具体代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tagobject.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QAbstractAnimation>
#include<QPropertyAnimation>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QGraphicsView *graphicsView  = new QGraphicsView() ;   //新建一个视图
    setCentralWidget(graphicsView);
    //新建一个场景,并将这个场景可视
   QGraphicsScene *_scene = new QGraphicsScene(this);  // 新建一个场景
    _scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    graphicsView->setScene(_scene);       
    graphicsView->show();
    tagObject *tg = new tagObject();    //创建图元对象
    tg->setColor(QColor(100,100,250));  // 设置颜色
    _scene->addItem(tg);                //将图元添加到场景中
    
    
   //加载动画机制,实现动画
    QPropertyAnimation * animation1 =  new QPropertyAnimation(tg,"pos");

    animation1->setDuration(2000);
    animation1->setStartValue(QPoint(0, 0));
    animation1->setEndValue(QPoint(300, 300));
    animation1->setEasingCurve(QEasingCurve::OutQuad);

    animation1->start(QAbstractAnimation::KeepWhenStopped);
}

MainWindow::~MainWindow()
{
    delete ui;
}


总结: Qt动画机制使用还是很简单。在项目中,为了实现更好的UI效果,往往会用到它。

Qt QGraphicsView是一个强大的图形界面框架,可以让用户轻松地在图形视图中显示和编辑图形元素。在Qt中,QGraphicsItem是QGraphicsScene中的基本元素,它可以表示任何类型的图形图元,如线条、多边形、文本、图像等。在本文中,我们将介绍如何使用Qt QGraphicsView创建自定义图元。 1. 创建自定义图元类 首先,我们需要创建一个自定义图元类,继承自QGraphicsItem。这个类可以是任何自定义的图形元素,比如矩形、圆形、多边形等。在这里,我们将创建一个简单的矩形图元类。 class MyRectItem : public QGraphicsItem { public: MyRectItem(QGraphicsItem *parent = nullptr); QRectF boundingRect() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; }; 2. 实现boundingRect和paint函数 在自定义图元类中,我们需要实现boundingRect和paint函数。boundingRect函数返回一个QRectF对象,用于定义图元的边界框。paint函数用于绘制图元。 QRectF MyRectItem::boundingRect() const { return QRectF(-50, -50, 100, 100); } void MyRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option) Q_UNUSED(widget) painter->setPen(QPen(Qt::black, 2)); painter->setBrush(QColor(255, 0, 0, 50)); painter->drawRect(-50, -50, 100, 100); } 3. 在场景中添加图元 在主程序中,我们可以创建一个QGraphicsScene对象,在其中添加自定义图元对象。然后,我们可以使用QGraphicsView来显示这个场景。 QGraphicsScene *scene = new QGraphicsScene; MyRectItem *rectItem = new MyRectItem; scene->addItem(rectItem); QGraphicsView *view = new QGraphicsView(scene); view->show(); 4. 运行程序 现在,我们可以运行程序,看到我们刚刚创建的矩形图元。可以通过移动、缩放、旋转等操作来编辑图元。 通过这个简单的例子,我们可以看到如何使用Qt QGraphicsView创建自定义图元。可以扩展这个例子来创建更复杂的图元,比如多边形、文本、图像等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值