[Interest] QGraphicsItemAnimation: only setPosAt() is permanent?

Kimmo Viitanen kviitanen at gmail.com
Sun Jan 15 22:22:49 CET 2012


I am trying to develop a simple game using Qt and specifically Qt 
Graphics View Framework. I am using a custom QGraphicsItem with
QGraphicsItemAnimation to move and turn the objects around the scene.
Now I'm running into an issue, though, because it seems that the 
transformations applied by QGraphicsItemAnimation are not permanent
on the applied-on object; only setPosAt() seems to have an effect of
permanently moving the object around.

Below is a complete example. If you run it and press a key multiple
times, you should see the object gradually move on the scene. However,
the rotation always starts from the same initial position, not from
the position it was last left.

Is this the intended behaviour? The QGraphicsItemAnimation documentation
doesn't mention anything about whether the transformations should be
permanent or not. In any case, it seems rather strange that some would
be permanent and others not; not exactly the least surprise situation.

I also ran into this issue: 
https://bugreports.qt.nokia.com/browse/QTBUG-13378
It mentions QGraphicsItemAnimation not honoring QGraphicsItem's
setTranformOriginPoint(). It is marked as 'important' but
the documentation makes no mention of this case either.

So this leads me to believe I might be using some soon-to-be-obsolete
technology. Should I be using some other way to animate my items?


Or am I simply doing something wrong? I would hope that's the case.
I'm using Qt 4.8.0 built from source on Linux Mint 11 64-bit.



#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsItemAnimation>
#include <QTimeLine>
#include <QRectF>
#include <QPointF>
#include <QApplication>

class MyItem : public QGraphicsItem
{
public:

     MyItem(QGraphicsItem* parent = 0)
         : QGraphicsItem(parent), m_animation(0)
     {
         m_timeline.setDuration(1000);
         m_timeline.setFrameRange(0, 25);
     }
     ~MyItem()
     {
         delete m_animation;
     }

     void paint(QPainter *painter, const QStyleOptionGraphicsItem 
*option, QWidget *widget)
     {
         painter->drawRect(0, 0, 25, 25);
     }

     QRectF boundingRect() const
     {
         return QRectF(0, 0, 25, 25);
     }

     void keyPressEvent(QKeyEvent *event)
     {
         if (m_animation)
             delete m_animation;

         m_animation = new QGraphicsItemAnimation;
         m_animation->setTimeLine(&m_timeline);
         m_animation->setItem(this);

         for (int ii = 0; ii <= 25; ++ii)
         {
             m_animation->setPosAt(ii / 25.0,
                                   QPointF(this->pos().x() + ii * 1, 
this->pos().y()));
             m_animation->setRotationAt(ii / 25.0, ii - (rotation() + ii 
* 5));
         }
         m_timeline.start();
     }

private:

     QGraphicsItemAnimation* m_animation;
     QTimeLine m_timeline;
};

int main(int argc, char* argv[])
{
     QApplication app(argc, argv);
     QGraphicsScene scene;
     QGraphicsView view(&scene);
     MyItem item;
     item.setPos(10, 10);
     scene.addItem(&item);

     item.setFlag(QGraphicsItem::ItemIsFocusable);
     scene.setFocusItem(&item);

     view.show();

     return app.exec();
}



Cheers,

Kimmo



More information about the Interest mailing list