Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 01:13, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Animating graphics item position in Qt

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.

Article Metadata
Code ExampleTested with
Devices(s): Symbian, Windows
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition, FP2
S60 5th Edition
S60 5th Edition
S60 3rd Edition FP2
S60 3rd Edition FP1
Keywords: QGraphicsItem, QPropertyAnimation
Created: tepaa (16 Dec 2009)
Last edited: hamishwillee (11 Oct 2012)



This series of snippets demonstrates how to implement nice animation effects into your Qt UI application. Qt 4.6 has Qt Animation Framework as a built-in feature. Documentation can be found here: [1]

This snippet shows how to implement QGraphicsItem and animate a graphics item's position.

See full example code here: File:Animation


Qt 4.6 has been installed.

Header file

The QPropertyAnimation class animates Qt properties. QPropertyAnimation interpolates over Qt properties.

#include <QtGui>
class PictureItem: public QObject, public QGraphicsPixmapItem
// Change picture position property for QPropertyAnimation
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
PictureItemType = UserType + 1
PictureItem(QSizeF size, const QPixmap& pixmap = 0, QObject* parent = 0);
// Animate position of this class
void animatePosition(const QPointF& start, const QPointF& end);
QRectF boundingRect() const;
int type() const
return PictureItemType;
public slots:
void animationFinished();
QSizeF m_size;

Source file

#include "mainwindow.h"
PictureItem::PictureItem(QSizeF size, const QPixmap& pixmap, QObject* parent) :
QObject(parent), QGraphicsPixmapItem(pixmap)
m_size = size;
QRectF PictureItem::boundingRect() const
return QRectF(0, 0, m_size.width(), m_size.height());
void PictureItem::animatePosition(const QPointF& start, const QPointF& end)
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "pos");
// 2 second duration animation
// position to start animation
// end position of animation
// easing curve
// Listen animation finished signal
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
// Start animation and delete QPropertyAnimation class on the end
void PictureItem::animationFinished()
// This slot is called when animation ends


PictureItem position can be animated by calling PictureItem::animatePosition().

Animation Framework snippet series

Full example code package

This page was last modified on 11 October 2012, at 01:13.
180 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.