Using QGraphicsItem with Qt Animation Framework

Qt Animation Framework is one power piece in Qt that helps to create smooth and slick animation with lot of ease. I still remember the pre Qt 4.6 days, where I used timers to run animations 🙂 . Animation Framework heavily relies on Qt Property System, in short it animated the properties. So what about QGraphicsItems like QGraphicsPixmapItem, QGraphicsTextItem etc. The basic problem is that they (except classes derived from QGraphicsObject) don’t have Qt property system as they are not QObject based. So enter QGraphicsTransform and its hierarchy namely QGraphicsRotation, QGraphicsScale.

QGraphicsRotation can be applied as a transformation on a graphics item using QGraphicsItem::setTransformations(). The applied transformation is animated, which in-turn animates the graphicsitem. Its that simple. Few lines of code speaks more than many lines of non-sense …

    graphicsRotation->setAxis(Qt::YAxis);
    graphicsRotation->setAngle(0);
    QList<QGraphicsTransform*> t;
    t.append(graphicsRotation);
    myGraphicsPixmapItem->setTransformations(t);
    ...
    // animate the applied rotation
    propertyAnimation->setTargetObject(graphicsRotation); 
    propertyAnimation->setPropertyName("angle");
    propertyAnimation->setStartValue(graphicsRotation->angle());     
    propertyAnimation->setEndValue(-90); 
    propertyAnimation->setDuration(duration); 
    propertyAnimation->start(); 


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s