Qt基于QGraphicsObject自定义图元并实现简单的动画
文章目錄
- Qt基于QGraphicsObject自定義圖元并實(shí)現(xiàn)簡單的動畫
- 舉例;
Qt基于QGraphicsObject自定義圖元并實(shí)現(xiàn)簡單的動畫
Qt 圖形的繪制 可以是QPainter方法直接繪制,另一種方法就是通過繼承QGraphicsItem類來定制自己的圖元類,這樣就可以根據(jù)自己的需求來制作它的形狀、顏色、屬性以及交互等。但是這樣的圖元會存在一個(gè)缺點(diǎn),那就是不能使用Qt的信號/槽機(jī)制,也不能使用Qt屬性系統(tǒng),也不能使用Qt的動畫框架實(shí)現(xiàn)動畫。
在Qt已經(jīng)考慮到了這些問題,QGraphicsObject應(yīng)運(yùn)而生。QGraphicsObject同時(shí)繼承于QObject和QGraphicsItem。這樣自定義圖元既可以獲得QGraphicsItem中寫好的圖元特性又能使用信號/槽機(jī)制方便信息傳遞,還可以使用Qt的動畫框架。
動畫框架的好處在于,在單線程的UI設(shè)計(jì)中,可以實(shí)現(xiàn)不同的圖元的各自的動畫效果。
舉例;
實(shí)現(xiàn)這樣的效果:在場景中添加自定義圖元,利用QQPropertyAnimation 動畫框架實(shí)現(xiàn)動畫。
過程:
1、新建一個(gè)的項(xiàng)目,命名可以自定義 如graphicObjectDemo。
2、添加類tagObject :
注意:如果該類要使用信號機(jī)制,記得一定要勾選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);// 各個(gè)點(diǎn)的坐標(biāo)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 函數(shù),主要是完成如下幾個(gè)任務(wù)創(chuàng)建場景,創(chuàng)建視圖,添加圖元,加載動畫。具體代碼如下:
#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() ; //新建一個(gè)視圖setCentralWidget(graphicsView);//新建一個(gè)場景,并將這個(gè)場景可視QGraphicsScene *_scene = new QGraphicsScene(this); // 新建一個(gè)場景_scene->setItemIndexMethod(QGraphicsScene::NoIndex);graphicsView->setScene(_scene); graphicsView->show();tagObject *tg = new tagObject(); //創(chuàng)建圖元對象tg->setColor(QColor(100,100,250)); // 設(shè)置顏色_scene->addItem(tg); //將圖元添加到場景中//加載動畫機(jī)制,實(shí)現(xiàn)動畫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; }總結(jié): Qt動畫機(jī)制使用還是很簡單。在項(xiàng)目中,為了實(shí)現(xiàn)更好的UI效果,往往會用到它。
總結(jié)
以上是生活随笔為你收集整理的Qt基于QGraphicsObject自定义图元并实现简单的动画的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现Qt日志功能并输出到文件
- 下一篇: 关于QMap的几点总结思考