Time for action – keeping multiple animations in sync
If you have a look at how the coins (their class being called Coin
) are created, you see similar structures. They inherit from QObject
and QGraphicsEllipseItem
and define two properties: opacity of type qreal
and rect
of type QRect
. This is done only by the following code:
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity) Q_PROPERTY(QRectF rect READ rect WRITE setRect)
No function or slot was added because we simply used built-in functions of QGraphicsItem
and "redeclared" them as properties. Then, these two properties are animated by two QPropertyAnimation
objects. One fades the coin out, while the other scales the coin in. To ensure that both animations get started at the same time, we use QParallelAnimationGroup
as follows:
QPropertyAnimation *fadeAnimation = /* set up */ QPropertyAnimation *scaleAnimation = /* set up */ QParallelAnimationGroup *group = new QParallelAnimationGroup(this); group->addAnimation(fadeAnimation); group...