PDA

View Full Version : Using QPropertyAnimation with QGraphicsPixmapItem



Luc4
26th March 2010, 15:22
Hi! Is it possible to use QPropertyAnimation with a QGraphicsPixmapItem? I read it is possible by creating a subclass and making it inherit from QObject as well. So, I created it and defined the property like:


class MyPixmap : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
Q_PROPERTY (qreal rotation READ rotation WRITE setRotation)
...

but when running the animation I get a crash. I made the same for a QGraphicsItem and it worked, but this isn't. Any idea why?
Thanks!

Lykurg
26th March 2010, 20:01
that is strange. Run a debugger and see where exactly your app crashes.

Luc4
26th March 2010, 20:47
I already tried that: it crashes when it executes the instruction:

QPropertyAnimation* anim = new QPropertyAnimation(item, "rotation");
I checked item and it seems it's my class correctly, not a NULL pointer. Everything seems OK to me... any idea? Thanks!

Lykurg
26th March 2010, 21:58
what does the backtrace say?

Lykurg
26th March 2010, 22:04
Well, it must be in your definition of the class. Following works fine on my system:
class MyPixmap : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
Q_PROPERTY (qreal rotation READ rotation WRITE setRotation)
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyPixmap *it = new MyPixmap();
QPropertyAnimation* anim = new QPropertyAnimation(it, "rotation");
qWarning() << anim->propertyName();
delete anim;
delete it;
return 0;
}

Luc4
28th March 2010, 19:36
I get: undefined reference to `vtable for MyPixmap'...

Lykurg
28th March 2010, 22:04
Well here is the full code. You have to rerun qmake:
#include <QtGui>

class MyPixmap : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
Q_PROPERTY (qreal rotation READ rotation WRITE setRotation)
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyPixmap *it = new MyPixmap();
QPropertyAnimation* anim = new QPropertyAnimation(it, "rotation");
qWarning() << anim->propertyName();
delete anim;
delete it;
return 0;
}

#include "main.moc"

Luc4
29th March 2010, 01:00
Yes, now its working... There must be an error in my implementation. I will try to find it. Anyway, another solution was to use a QGraphicsProxyWidget. From the point of view of the performance (of animations for instance), is there any difference between using QGraphicsPixmapItems or other types of items like QGraphicsProxyWidgets? Sometimes I wonder if it would be better to use widgets or items in QGraphicsView to play animations.
Thanks for the information!

Lykurg
29th March 2010, 10:47
I would go for a normal item since it is not so big and heavy like a QGraphicsWidget. But if you only use a small number it does not make a big difference.