PDA

View Full Version : Animation of QGraphicsItem in QGraphicsView



mirelon
17th December 2009, 01:59
I want to make an animation with QGraphicsItem. I know it is possible with QGraphicsItemAnimation, but I want to join more animations with QSequentialAnimationGroup. In order to do this, child animations have to inherit QAbstractAnimation. In docs, there is an example how to do this with QPropertyAnimation. The key to victory is macro Q_PROPERTY as explained here: http://doc.trolltech.com/solutions/4/qtanimationframework/animation.html#animations-and-the-graphics-view-framework.

I tried to implement. It compiled succesfully, but in runtime, when creating QPropertyAnimation, the error appeared:

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

But it is declared in the macro, so where is the problem?



#ifndef CARDITEM_H
#define CARDITEM_H

class CardItem;

#include <QGraphicsItem>
#include <QMouseEvent>
#include <QDebug>
#include "deskview.h"

class CardItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
public:
CardItem();
void mousePressEvent(QGraphicsSceneMouseEvent *event);
int cid;
};

#endif // CARDITEM_H





...
CardItem* ci = new CardItem();
ci->setPixmap(rub);
ci->setPos(-100,150);
ci->setZValue(10);
graphicsView->scene()->addItem(ci);
QPropertyAnimation an(ci,"setPos");
...

Lykurg
17th December 2009, 09:19
QPropertyAnimation an(ci,"setPos");

You must use the name of the property!
QPropertyAnimation an(ci,"pos");

mirelon
18th December 2009, 02:49
Thank you very much, now I am going on with my project. But another question arises. When some animation is going near the border of graphicsView, it automatically moves the whole scene and the scrollbars appear.

I searched for it, but I dont have correct keywords for it, i think it should be some property of graphics view. I want something like the coordinate system in graphics view is fixed, so it dont move on its own.

Lykurg
18th December 2009, 08:45
Then you have to set a sceen rect by your own. Then Qt wont adjust the size automatically. And you might want to positioning the scene on the top rather than in the middle.

mirelon
19th December 2009, 16:25
Ok, i managed to do it...but not while i want to enable resizing of the window.
I have:

main window
central widget (vertical layout, so i can set size policy)
graphics view
graphics scene

and when i resize main window, the graphics view should be also resized
it can be done with size policy "expanding"

but i also want to have graphics scene fixed 800*600 (i have hard-coded positions of animations in it, so it must be this size), so resizing will zoom the scene.

how can i do that?