Qt图形视图框架(六) 为Item应用属性动画

网友投稿 313 2022-11-27

Qt图形视图框架(六) 为Item应用属性动画

为Item应用属性动画

以下代码介绍了系统自带Item以及自定义Item的动画实现,注意使用QPropertyAnimation的类必须继承自QGraphicsObject

头文件(自定义Item):

#ifndef ITEMS_H#define ITEMS_H#include class ColorFulCircle : public QGraphicsObject{ //实现属性所用到的宏 Q_PROPERTY(QColor color MEMBER m_color READ color WRITE setColor NOTIFY colorChanged) Q_OBJECTpublic: ColorFulCircle(QGraphicsItem *parent = 0); ColorFulCircle(int w, int h, QColor color, QGraphicsItem *parent = 0); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);signals: void colorChanged();public: QColor color(); void setColor(QColor color);protected: int w, h; QColor m_color;};#endif // ITEMS_H

其源文件:

#include "items.h"#include ColorFulCircle::ColorFulCircle(QGraphicsItem *parent) :QGraphicsObject(parent) {}ColorFulCircle::ColorFulCircle(int w, int h, QColor color, QGraphicsItem *parent) :QGraphicsObject(parent), w(w), h(h), m_color(color) {}QRectF ColorFulCircle::boundingRect() const { return QRectF(0, 0, w, h);}void ColorFulCircle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setRenderHint(QPainter::Antialiasing, true); QRadialGradient radial(1.0*w/2, 1.0*h/2, 1.0*w/2, 1.0*w/2, 1.0*h/2); radial.setColorAt(1, Qt::white); radial.setColorAt(0, m_color); painter->setPen(Qt::white); painter->setBrush(radial); painter->drawEllipse(0, 0, w, h);}QColor ColorFulCircle::color() { return m_color;}void ColorFulCircle::setColor(QColor color) { m_color = color; update();}

main:

#include #include #include "items.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); QGraphicsScene scene(0, 0, 800, 800); QGraphicsView view(&scene); view.setFixedSize(1000, 1000); view.setMaximumSize(1000, 1000); view.setMinimumSize(1000, 1000); QFont font = a.font(); font.setPointSize(20); auto text = new QGraphicsTextItem("Hello"); text->setFont(font); text->setPos(100, 100); text->setZValue(1); scene.addItem(text); QPropertyAnimation anim(text, "pos"); anim.setDuration(5000);//设置动画周期 anim.setStartValue(QPointF(0, 0)); anim.setEndValue(QPointF(500, 500)); anim.setEasingCurve(QEasingCurve(QEasingCurve::OutCubic));//设置变换曲线 anim.setLoopCount(4); anim.start(); QPropertyAnimation animRotate(text, "rotation"); animRotate.setDuration(5000); animRotate.setStartValue(0); animRotate.setEndValue(360); animRotate.setLoopCount(4); anim.setEasingCurve(QEasingCurve(QEasingCurve::InCurve)); animRotate.start(); //自定义Item auto cc = new ColorFulCircle(100, 100, Qt::red); cc->setPos(500, 500); scene.addItem(cc); QPropertyAnimation ccAnim(cc, "pos"); ccAnim.setDuration(5000); ccAnim.setStartValue(QPointF(500, 500)); ccAnim.setEndValue(QPointF(0, 0)); ccAnim.setLoopCount(4); ccAnim.start(); QPropertyAnimation ccAnimColor(cc, "color"); ccAnimColor.setDuration(5000); ccAnimColor.setStartValue(QColor(Qt::red)); ccAnimColor.setEndValue(QColor(Qt::yellow)); ccAnimColor.setLoopCount(4); ccAnimColor.start(); view.show(); return a.exec();}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:开放网络是怎么一回事
下一篇:通过Java修改游戏存档的实现思路
相关文章

 发表评论

暂时没有评论,来抢沙发吧~