使用现有的Q_PROPERTY动画化继承QObject的QGraphicsItem [英] Using an existing Q_PROPERTY to animate QGraphicsItem inheriting QObject

查看:210
本文介绍了使用现有的Q_PROPERTY动画化继承QObject的QGraphicsItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个类,其中一个MainWindow通过另一个类在QGraphicsView中绘制一个圆(打算成为按钮!). MyCircle类继承自QObject和QGraphicsItem,因为我想制作动画.

I have 2 classes, one MainWindow in which we draw in a QGraphicsView a circle (which intend to become a button !) created thanks to an other class. The class MyCircle inherits from QObject and QGraphicsItem since I want to make animation.

我的问题如下:

我的目标是首先在我的图形上制作一个简单的动画:先缩小它,然后再恢复到原始大小.因此,我想我应该使用QObject类中已经存在的属性几何.

My goal is first to make a simple animation on my drawing : make it smaller then it goes back to the original size. So I suppose I should use the property geometry, already existing in the QObject class.

为此,我在MainWindow.ccp中编写

To do this I write in my MainWindow.ccp

animationBoutonRondTaille = new QPropertyAnimation(roundButton, "geometry");
animationBoutonRondTaille->setDuration(1000);
animationBoutonRondTaille->setKeyValueAt(0, QRect(-100, -100, 200, 200));
animationBoutonRondTaille->setKeyValueAt(0.5, QRect(-80,-80,160,160));
animationBoutonRondTaille->setKeyValueAt(1, QRect(-100, -100, 200, 200));
animationBoutonRondTaille -> start();

如果我不包括

class MyCircle : public QObject, public QGraphicsItem
{
    Q_OBJECT
    Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry)
/.../
}

我收到以下错误消息:

QPropertyAnimation: you're trying to animate a non-existing property geometry of your QObject

但如果我这样做,我就会得到这个:

But if I do, i got this one :

'class MyCircle' has no member named 'geometry'/'setgeometry'

如果我必须自己定义所有的geometry属性,那么继承QObject的目的是什么?

What is the purpose of inheriting QObject if I have to define all by myself the geometry property ?

希望您能为我提供帮助,对不起,如果我的问题含糊不清,这对我来说是第一个,所以我真的不知道您的期望.

Hope you can help me, and sorry if my question is vague, it's the first for me so I don't really know what you expect.

非常感谢您花时间回答.

Thanks a lot if you take time to answer.

推荐答案

您必须实现geometrysetGeometry方法,Q_PROPERTY用于使用setProperty("geometry", some_value)property("geometry")函数调用函数在QPropertyAnimation中以及Setter函数(在本例中为setGeometry)中内部使用的图形,必须调用update()来更新图形.

You have to implement the geometry and setGeometry methods, the Q_PROPERTY are used to call functions using the setProperty("geometry", some_value) and property("geometry") functions that are used internally in QPropertyAnimation, as well as in the Setter function (in this case setGeometry) you must call update() to update the graph.

mycircle.h

#ifndef MYCIRCLE_H
#define MYCIRCLE_H

#include <QGraphicsItem>
#include <QObject>
#include <QPainter>
#include <QStyleOptionGraphicsItem>

class MyCircle : public QObject, public QGraphicsItem
{
    Q_OBJECT
    Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry)
    Q_INTERFACES(QGraphicsItem)
public:
    explicit MyCircle(QObject *parent = nullptr);

    QRect geometry() const;
    void setGeometry(const QRect &value);

    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);

private:

    QRect rect;
};

#endif // MYCIRCLE_H

mycircle.cpp

#include "mycircle.h"

MyCircle::MyCircle(QObject *parent) : QObject(parent)
{
    rect = QRect(0, 0, 100, 100);
}

QRect MyCircle::geometry() const
{
    return rect;
}

void MyCircle::setGeometry(const QRect &value)
{
    if(rect!=value){
        rect = value;
        update();
    }
}

QRectF MyCircle::boundingRect() const
{
    return QRectF(rect);
}


void MyCircle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
           QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    painter->setBrush(QBrush(Qt::red));
    painter->drawEllipse(rect);
}

可以在此处

这篇关于使用现有的Q_PROPERTY动画化继承QObject的QGraphicsItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆