CopperSpice API  1.8.2


class  QAbstractAnimation
 Base class for all animations More...
class  QAnimationGroup
 Abstract base class for groups of animations More...
class  QEasingCurve
 Easing curves for controlling animation More...
class  QParallelAnimationGroup
 Parallel group of animations More...
class  QPauseAnimation
 Pause for QSequentialAnimationGroup More...
class  QPropertyAnimation
 Animates properties More...
class  QSequentialAnimationGroup
 Sequential group of animations More...
class  QTimeLine
 Timeline for controlling animations More...
class  QVariantAnimation
 Provides an abstract base class for animations More...

Detailed Description

The animation technology aims to provide an easy way to create an animated, smooth graphical user interface. By animating properties this technology provides freedom to animate widgets and other QObjects. Animations can also be used with the Graphics View system.


This section contains a high level overview about the animation architecture and how it is used. The following diagram shows the most important classes in the animation technology.

The foundation for animations consists of the base class QAbstractAnimation, and its two subclasses QVariantAnimation and QAnimationGroup. QAbstractAnimation is the parent class of all animations. It represents basic properties that are common for all animations, notably the ability to start, stop, and pause an animation. It is also receives the time change notifications.

The QPropertyAnimation class inherits from QVariantAnimation and performs animations of a CopperSpice property, which is part of the Meta Object System. This class performs an interpolation over the property using an easing curve. So when you want to animate a value, you can declare it as a property and make your class a QObject.

Complex animations can be constructed by building a tree structure of QAbstractAnimations. The tree is built by using QAnimationGroups, which function as containers for other animations. The groups are subclasses of abstractions, so groups can themselves contain other groups.

The animation technology can be used on its own, however it was also designed to be used with the state machine classes. QStateMachine provides a special state that can play an animation. A QState can also set properties when the state is entered or exited and this special animation state will interpolate between these values when given a QPropertyAnimation.

Animations are controlled by a global timer which sends updates to all animations which are playing.

Animating Properties

As mentioned in the previous section, the QPropertyAnimation class can interpolate over CopperSpice properties. It is this class that should be used for animation of values, in fact its parent class QVariantAnimation is an abstract class, and can not be used directly.

Here is a simple example.

QPushButton button("Animated Button");;
QPropertyAnimation animation(&button, "geometry");
animation.setStartValue(QRect(0, 0, 100, 30));
animation.setEndValue(QRect(250, 250, 100, 30));

This code will move button from the top left corner of the screen to the position (250, 250) in 10 seconds (10000 milliseconds).

The example above will do a linear interpolation between the start and end value. It is also possible to set values situated between the start and end value. The interpolation will then go by these points.

QPushButton button("Animated Button");;
QPropertyAnimation animation(&button, "geometry");
animation.setKeyValueAt(0, QRect(0, 0, 100, 30));
animation.setKeyValueAt(0.8, QRect(250, 250, 100, 30));
animation.setKeyValueAt(1, QRect(0, 0, 100, 30));

In this example the animation will take the button to (250, 250) in 8 seconds, and then move it back to its original position in the remaining 2 seconds. The movement will be linearly interpolated between these points.

You also have the possibility to animate values of a QObject that is not declared as a CopperSpice property. The only requirement is that this value has a setter. You can then subclass the class containing the value and declare a property that uses this setter. Each CopperSpice property requires a getter, so you will need to provide one if it is not defined.

class MyGraphicsRectItem : public QObject, public QGraphicsRectItem
CS_PROPERTY_READ(geometry, geometry)
CS_PROPERTY_WRITE(geometry, setGeometry)

In the above code example, we subclass QGraphicsRectItem and define a geometry property. We can now animate the widgets geometry even if QGraphicsRectItem does not provide the geometry property.

For a general introduction to the property system refer to the properties overview.

Animations and the Graphics View System

When you want to animate QGraphicsItems, you also use QPropertyAnimation. However, QGraphicsItem does not inherit QObject. A good solution is to subclass the graphics item you wish to animate. This class will then also inherit QObject. This way, QPropertyAnimation can be used for QGraphicsItems. The example below shows how this is done. Another possibility is to inherit QGraphicsWidget, which already is a QObject.

class Pixmap : public QObject, public QGraphicsPixmapItem

As described in the previous section, we need to define properties we wish to animate. The QObject must be the first class inherited.

Easing Curves

As mentioned, QPropertyAnimation performs an interpolation between the start and end property value. In addition to adding more key values to the animation, you can also use an easing curve. Easing curves describe a function that controls how the speed of the interpolation between 0 and 1 should be, and are useful if you want to control the speed of an animation without changing the path of the interpolation.

QPushButton button("Animated Button");;
QPropertyAnimation animation(&button, "geometry");
animation.setStartValue(QRect(0, 0, 100, 30));
animation.setEndValue(QRect(250, 250, 100, 30));

Here the animation will follow a curve that makes it bounce like a ball as if it was dropped from the start to the end position. QEasingCurve has a large collection of curves for you to choose from. These are defined by the QEasingCurve::Type enum. If you are in need of another curve, you can also implement one yourself, and register it with QEasingCurve.

Putting Animations Together

An application will often contain more than one animation. For instance, you might want to move more than one graphics item simultaneously or move them in sequence after each other.

The subclasses of QAnimationGroup (QSequentialAnimationGroup and QParallelAnimationGroup) are containers for other animations so that these animations can be animated either in sequence or parallel. The QAnimationGroup is an example of an animation that does not animate properties, but it gets notified of time changes periodically. This enables it to forward those time changes to its contained animations, and thereby controlling when its animations are played.

Let's look at code examples that use both QSequentialAnimationGroup and QParallelAnimationGroup, starting off with the latter.

QPushButton *bonnie = new QPushButton("Bonnie");
QPushButton *clyde = new QPushButton("Clyde");
QPropertyAnimation *anim1 = new QPropertyAnimation(bonnie, "geometry");
// Set up anim1
QPropertyAnimation *anim2 = new QPropertyAnimation(clyde, "geometry");
// Set up anim2

A parallel group plays more than one animation at the same time. Calling its start() function will start all animations it governs.

QPushButton button("Animated Button");;
QPropertyAnimation anim1(&button, "geometry");
anim1.setStartValue(QRect(0, 0, 100, 30));
anim1.setEndValue(QRect(500, 500, 100, 30));
QPropertyAnimation anim2(&button, "geometry");
anim2.setStartValue(QRect(500, 500, 100, 30));
anim2.setEndValue(QRect(1000, 500, 100, 30));

As you no doubt have guessed, QSequentialAnimationGroup plays its animations in sequence. It starts the next animation in the list after the previous is finished.

Since an animation group is an animation itself, you can add it to another group. This way, you can build a tree structure of animations which specifies when the animations are played in relation to each other.

Animations and States

When using a State Machine, we can associate one or more animations to a transition between states using a QSignalTransition or QEventTransition class. These classes are both derived from QAbstractTransition, which defines the convenience function addAnimation() that enables the appending of one or more animations triggered when the transition occurs.

We also have the possibility to associate properties with the states rather than setting the start and end values ourselves. Below is a complete code example that animates the geometry of a QPushButton.

QPushButton *button = new QPushButton("Animated Button");
QState *state1 = new QState(machine);
state1->assignProperty(button, "geometry", QRect(0, 0, 100, 30));
QState *state2 = new QState(machine);
state2->assignProperty(button, "geometry", QRect(250, 250, 100, 30));
QSignalTransition *transition1 = state1->addTransition(button,
SIGNAL(clicked()), state2);
transition1->addAnimation(new QPropertyAnimation(button, "geometry"));
QSignalTransition *transition2 = state2->addTransition(button,
SIGNAL(clicked()), state1);
transition2->addAnimation(new QPropertyAnimation(button, "geometry"));