Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
animation.qdoc
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \group animation
6 \brief Provides an easy way for creating animated GUIs.
7 \title Animation Framework
8
9 This page lists classes belonging to \l{Qt Core}'s
10 \l{The Animation Framework}{animation framework}.
11
12*/
13
14/*!
15 \page animation-overview.html
16 \title The Animation Framework
17 \ingroup qt-gui-concepts
18
19 \brief An overview of the Animation Framework
20
21 \ingroup frameworks-technologies
22
23 \keyword Animation
24
25 The animation framework provides an easy way to animate your GUI elements.
26 It enables you to animate a Qt property value of a widget or QObject.
27 Most of the features offered by the framework are also available in
28 \l{Qt Quick}, where it's possible to define animations in a declarative way.
29
30 This overview explains the framework's architecture, with examples that
31 demonstrate the common techniques used for animating QObject and
32 GUI elements.
33
34 \tableofcontents
35
36 \section1 The Animation architecture
37
38 The following diagram shows the most important classes provided by the
39 framework:
40
41 \image animations-architecture.png
42
43 It includes the QAbstractAnimation class, which provides the
44 necessary foundation for animations. This class defines the
45 generic properties for all animations supported by the framework.
46 For example, the ability to start, stop, and pause an animation. The
47 class also receives the time change notifications.
48
49 The framework further provides the QVariantAnimation and
50 QAnimationGroup classes, which build on their base case, QAbstractAnimation.
51 Next in the hierarchy is QPropertyAnimation, which is derived from
52 QVariantAnmiation, and it lets you animate a Qt property of a widget or
53 QObject. The class performs interpolation on the property value using an
54 easing curve. With these in place, you just need a QObject class with a
55 Qt property value that you can animate.
56
57 \note It is required that the target object you are animating is a QObject
58 or its subclass. This is necessary as the animation framework depends on the
59 \l{Meta-Object System}{meta-object system} for all the information about the
60 object it is animating.
61
62 Complex animations can be constructed by building a tree structure
63 of \l{QAbstractAnimation}s, where the tree is a QAnimationGroup that
64 contains other animations. These animation groups can also contain
65 subgroups representing different groups or animations, such as
66 QParallelAnimationGroup and QSequentialAnimationGroup.
67
68 Behind the scenes, all animations are controlled by a global
69 timer, which sends \l{QAbstractAnimation::updateCurrentTime()}{updates} about
70 all animations that are running.
71
72 For detailed information of these individual classes' and their roles in
73 the framework, refer to their documentation.
74
75 \section1 Classes offered by the framework
76
77 These classes provide the necessary infrastructure to create both simple and
78 complex animations.
79
80 \annotatedlist animation
81
82 \section1 Animating Qt properties
83
84 As the QPropertyAnimation class can interpolate on Qt properties, it is
85 used often. In fact, its superclass---QVariantAnimation---provides an
86 abstract implementation of \l{QVariantAnimation::}{updateCurrentValue()},
87 which does not change any value unless you change it on the
88 \l{QVariantAnimation::valueChanged()}{valueChanged signal}.
89
90 The framework lets you animate the Qt properties of the existing
91 classes in Qt. For example, the QWidget class---can be embedded in
92 a QGraphicsView---has properties for its bounds, colors, and so on.
93 The following example demonstrates how you can animate a QPushButton
94 widget:
95
96 \snippet code/src_corelib_animation_qpropertyanimation.cpp 0
97
98 The example animates the \c pos Qt property of a QPushButton, to move
99 it from the top--left corner of the screen to the end position (250, 250),
100 in 10 seconds (10000 milliseconds).
101
102 It uses the linear interpolation method to control the speed of
103 animation between the start and end values. Try adding another value
104 in--between the start and end value to see how they are interpolated.
105 This time use the QPropertyAnimation::setKeyValueAt function to add
106 these values:
107
108 \code
109 ...
110 anim->setDuration(10000);
111 anim->setKeyValueAt(0, QPoint(0, 0));
112 anim->setKeyValueAt(0.8, QPoint(250, 250));
113 anim->setKeyValueAt(1, QPoint(0, 0));
114 ...
115 \endcode
116
117 In this example, the animation moves the button to
118 (250, 250) in 8 seconds, and moves it back to its original position in
119 the remaining 2 seconds. The button's movement is linear-interpolated
120 between these points.
121
122 You can also animate a QObject's value that is not declared as a Qt
123 property, if the value has a setter method. In such cases, derive
124 a new class from the class that contains the value, and add a Qt property
125 for that value with the setter.
126
127 \note Each Qt property requires a getter also, so you should provide a
128 getter if that is not defined.
129
130 \code
131 class MyGraphicsRectItem : public QObject, public QGraphicsRectItem
132 {
133 Q_OBJECT
134 Q_PROPERTY(QPointF pos READ pos WRITE setPos)
135 };
136 \endcode
137
138 In this example, the \c MyGraphicsRectItem derives from
139 QGraphicsRectItem and QObject, and defines the \c pos property. You can
140 animate the item's \c pos even if QGraphicsRectItem does not provide
141 the \c pos property.
142
143 For a general introduction to the Qt property system, refer to
144 \l{Qt's Property System}.
145
146 \section1 Animations and the Graphics View Framework
147
148 QPropertyAnimation can also be used to animate a QGraphicsItem, which does
149 not inherit QObject. In such cases, you derive a class from the graphics
150 item that you want to animate. This derived class should also inherit form
151 QObject to enable using QPropertyAnimation on a QGraphicsItem. The
152 following example shows how this is done:
153
154 \code
155 class Pixmap : public QObject, public QGraphicsPixmapItem
156 {
157 Q_OBJECT
158 Q_PROPERTY(QPointF pos READ pos WRITE setPos)
159 ...
160 }
161 \endcode
162
163 \note You can also derive from QGraphicsWidget, which already is a
164 QObject.
165
166 As described in the previous section, you need to define
167 properties that you want to animate. The derived class must inherit
168 from QObject first as the meta-object system requires it.
169
170 \section1 Easing curves
171
172 A QPropertyAnimation performs linear interpolation
173 between the start and end property values. In addition to adding more key
174 values to the animation, you can also choose an easing curve to control the
175 speed of interpolation between 0 and 1, without changing the
176 path.
177
178
179 \snippet code/src_corelib_animation_qpropertyanimation.cpp easing-curve
180
181 In this example, the animation follows a curve that makes the
182 \c button bounce like a ball. QEasingCurve offers a large collection of curves
183 to choose from the QEasingCurve::Type enum. If you want
184 to use another curve that is not available, implement one yourself and
185 register it with QEasingCurve.
186
187 \section1 Grouping animations
188
189 An application often contains more than one animation. For
190 example, it wants to move more than one graphics item
191 simultaneously or move them in sequence after each other.
192
193 The subclasses of QAnimationGroup---QSequentialAnimationGroup and
194 QParallelAnimationGroup---are containers for other animations so
195 that these animations can be animated either in sequence or
196 parallel. The QAnimationGroup does not animate properties, but it
197 gets notified of time changes periodically. This enables it to
198 forward those time changes to the animation groups, which control when
199 their animations are played.
200
201 The two following examples demonstrate the use of both
202 QSequentialAnimationGroup and QParallelAnimationGroup:
203
204 \snippet code/src_corelib_animation_qpropertyanimation.cpp animation-group1
205
206 A parallel group plays more than one animation at the same time.
207 Its \l{QAbstractAnimation::}{start()} function starts all
208 animations that are part of the group.
209
210 \snippet code/src_corelib_animation_qpropertyanimation.cpp animation-group2
211
212 As the name suggests, a QSequentialAnimationGroup plays
213 its animations in sequence. It starts the next animation in
214 the list after the previous finishes.
215
216 A group is an animation itself, so you can add
217 it to another group. This way, building an animation tree, which define
218 when the animations are played in relation to each other.
219
220 \section1 Object ownership
221
222 A QPropertyAnimation should always have a parent that controls
223 its lifespan. A typical application may include several animations that
224 are grouped, where the animation group takes ownership of those animations.
225 An independent QPropertyAnimation must be explicitly assigned a parent to
226 control its lifespan. In the following example, you can see that an
227 independent QPropertyAnimation has the QApplication instance as its
228 parent:
229
230 \snippet code/src_corelib_animation_qpropertyanimation.cpp 0
231
232 \note You can also control the animation's lifespan by choosing a
233 \l{QAbstractAnimation::DeletionPolicy}{delete policy} while starting it.
234*/