Solved Error on creating QPropertyAnimation object
-
Header file :
#ifndef MYRECT_H #define MYRECT_H #include <QGraphicsItem> #include <QPropertyAnimation> class MyRect:public QGraphicsItem { Q_PROPERTY(QRect nrect READ nRect WRITE setNrect) public: MyRect(); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const; QRect nRect()const; void setNrect(const QRect &rect); protected: private: QRect mRect; QPropertyAnimation *anime; }; #endif // MYRECT_H
In cpp file :
anime = new QPropertyAnimation(this,"nrect"); //error anime->setDuration(2000); anime->setStartValue(QRect(0,0,10,10)); anime->setEndValue(QRect(0,0,50,50)); anime->start(); QObject::connect(anime,&QPropertyAnimation::valueChanged,[=](){update();});
-
QGraphicsItem is not a QObject. It cannot have Qt properties.
So, either use multiple inheritance to inherit from QGraphicsItem and QObject, or use Q_GADGET macro (and don't pass any parent to QPropertyAnimation).
-
@sierdzio
Thanks.How should i create QPropertyAnimation object ? -
class MyRect:public QGraphicsItem,public QObject { Q_PROPERTY(QRect nrect READ nRect WRITE setNrect)
It seems QPropertyAnimation doen't know nrect
QPropertyAnimation: you're trying to animate a non-existing property nrect of your QObject -
Do you have
Q_OBJECT
macro in your header file? -
@sierdzio
If i add Q_OBJECT it generates errors.
I made a new class from QObject and it solves the problem. -
@behruz-montazeri said in Error on creating QPropertyAnimation object:
@sierdzio
If i add Q_OBJECT it generates errors.What errors? You need to fix them.
There is no way a QObject with properties will work without the macro, you have to have it.
-
@sierdzio
Also instead of QGraphicsItem We can use QGraphicsObject