Moving a QGraphicsItem by using QPropertyAnimation
I am planning to move a circle in the graphics view but it doesn't work. Here is my source code:
circle.h
Code:
#ifndef CIRCLE_H
#define CIRCLE_H
#include <QGraphicsItem>
#include <QPainter>
{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos
) public:
Circle();
~Circle();
protected:
};
#endif // CIRCLE_H
dialog.h
Code:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtGui>
#include <QPropertyAnimation>
#include "circle.h"
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private:
Ui::Dialog *ui;
Circle *circle;
QPropertyAnimation *animation;
};
#endif // DIALOG_H
circle.cpp
Code:
#include "circle.h"
Circle::Circle()
{
}
Circle::~Circle()
{
}
QRectF Circle
::boundingRect() const {
return QRectF(20,
100,
50,
50);
}
{
brush.setStyle(Qt::SolidPattern);
painter->setPen(pen);
painter->setBrush(brush);
painter->drawEllipse(rect);
}
dialog.cpp
Code:
#include "dialog.h"
#include "ui_dialog.h"
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
circle = new Circle;
scene->addItem(circle);
animation = new QPropertyAnimation(circle, "geometry", NULL);
animation->setDuration(5000);
animation
->setEndValue
(QRect(500,
100,
150,
150));
animation->start();
}
Dialog::~Dialog()
{
delete ui;
}
Can anyone please tell me how to solve this thing? Thanks in advance. :D
Re: Moving a QGraphicsItem by using QPropertyAnimation
Your property is called "pos" .
You are trying to animate non existant property "geometry"
Cheers,
_
Re: Moving a QGraphicsItem by using QPropertyAnimation
Neither QObject nor QGraphicsItem seem to have the 'geometry' property you're assigning to that animation controller. So it's not defined anywhere, and this is probably why nothing happens.
The documentation specifies the properties of QObject-based classes:
http://doc.qt.io/qt-5/qobject.html
http://doc.qt.io/qt-5/qgraphicsitem.html
So in your case you will probably have to write something like this:
Code:
Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry
)
void setGeometry( QRectF& newRect )
{
geometry = newRect // Copy.
setPos( newRect.topLeft() );
}
{
return geometry;
}
Also, make sure you're only using QRectF values (for example in the 'setEndValue' function).