View Full Version : How to change the SIZE of a graphicsItem during animation(QGraphicsItemAnimation).

29th August 2011, 13:21
I want to change the SIZE of a graphicsItem (eclipse) during its animation using QGraphicsItemAnimation.

The Eclipse is drawn from the below code:

QRectF Node::boundingRect() const
qreal adjust = 2;
return QRectF( -10 - adjust, -10 - adjust,
23 + adjust, 23 + adjust);

void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
painter->setPen(QPen(Qt::red, 0));
painter->drawEllipse(-10, -10, 20, 20);
Eclipse(Node) of above size got created.

Now in QGraphicsItemAnimation i want to change it's size to (-20,-20,40,40).

Could you please help me in changing the size of the eclipse. what functions can be used.


29th August 2011, 13:35
just use member variables instead of hardcoded values. Write a setter method to set these variables whenever you need. Remember to call prepareGeometryChange() before changing these variables in the setter method.

29th August 2011, 15:38

Thanks for the suggestion, partial problem is resolved.
I have another query regarding this.
I have n no. of nodes(eclipses) created from the same boundingrect and paint function, but these nodes(eclipses) are placed at different points in a scene.

Q. i want to change the size of all nodes(eclipses) simultaneously (parallel animation) by some random values +/- their width and height.
is there any way to do paralel animation in QGraphicsItemAnimation.

Thanks in advance,

29th August 2011, 18:48
Why don't you use the animation framework instead of QGraphicsItemAnimation?

30th August 2011, 08:27
I have used QParallelAnimationGroup for parallel animation of n buttons. But i think QParallelAnimationGroup only supports QWIdgets and not graphicsitems. This was the reason i switched to QGraphicsItemAnimation to animate GraphicsItems.

Please let me know if there is any way to do parallelanimation for graphicsitems in QParallelAnimation/QGraphicsItemAnimation.
Or can we convert GraphicsItem to QWidget and use QParallelAnimationGroup to animate.

Correct me if i m leading in wrong direction.


30th August 2011, 08:52
I have used QParallelAnimationGroup for parallel animation of n buttons. But i think QParallelAnimationGroup only supports QWIdgets and not graphicsitems.
No, that's not true. Have a look at the "animated tiles" demo.

31st August 2011, 07:54

I have gone through the animated tiles example, thanks for the suggestion.
In this example the animation is applied on pos of a pixmap.
Now, if i want to change the size of this pixmap through animation, what QtProperty should be taken care of.

Below is the code change that i did to change the size of pixmap.

class Pixmap : public QObject, public QGraphicsPixmapItem
// Q_PROPERTY(QPointF pos READ pos WRITE setPos)
Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry)

* i replaced pos with geometry.

After changing some code, below error occured.

Error: geometry/setGeometry not declared in scope.

My understanding:
setGeometry is not used in right place/not the right property that can change the size of pixmap/graphicitem.

Could you please let me know what code and where should i change the code to get the proper output.

Solution to this problem may solve my entire problem.


31st August 2011, 08:40
QGraphicsPixmapItem doesn't have "geometry" and "setGeometry" methods. You'll have to implement them if you want to have such a property. However I don't see how this would work since the pixmap is of fixed size. Maybe you want to scale and not resize the item? If so, then it will be easiest to use QGraphicsScale which already has appropriate properties defined.

31st August 2011, 10:00
Here is the main problem that i m trying to resolve.

In a graph i have nodes and edges. Initially the size of nodes and edges are fixed. I have to change the size of nodes as per some random data from a file or from an array. The size of all nodes should get change simultaneously along with the size/length of edges connecting them to give proper animation effect.

To Solve this problem i used "elasticnodes" example and convert this moving node to static graph. i removed their calculate force function so that nodes should not move automatically after draging any node.

Currently i have nodes and edges well connected to each other, with the help of a mouse i can change the positon of nodes, connections dont get distorted after changing the positions and gives elastic effects.

I tried scaleing a node and it was working fine, but connected edges were not changing their length and connections were distorted. so, i planned to change the size of nodes and shift their positions to give proper effect of scaling and as per my understanding connected edges should/will change their length as per the node positions.

Road Blocks:
1. Unable to change the size of nodes.
2. Scaling the nodes distorts the connections.
3. Unable to do all above simultaneously/ parallelanimation on graphicitems.

Please guide me on this, so that i can step in right direction and reslove this issue.

thanks in advance for you attention,


31st August 2011, 10:37
Then why are you using pixmap items?

Here is a small demo:

#include <QtGui>

class Item : public QGraphicsObject {
Q_PROPERTY(qreal size READ size WRITE setSize)
Item(QGraphicsItem *parent = 0) : QGraphicsObject(parent) {
void setSize(qreal s) {
if(m_rect.width()==s) return;
m_rect = QRectF(-s/2,-s/2, s, s);
qreal size() const { return m_rect.width(); }
QRectF boundingRect() const {
return m_rect;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) {
QRectF m_rect;

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view;
QParallelAnimationGroup *animGrp = new QParallelAnimationGroup(&scene);
for(int i=0;i<10;++i) {
Item *item = new Item;
item->setPos(qrand() % 400, qrand()%400);
QPropertyAnimation *anim = new QPropertyAnimation(item, "size", animGrp);

return app.exec();

2nd September 2011, 10:41
Thanks for the pointers.
Problem is resolved.
I used pixmap because i was trying to get solution of problem by changing the properties of a graphicsitem,and was trying to learn by doing hit and trial.
once again thanks for your help.