QAbstractAnimation Class
The QAbstractAnimation class is the base of all animations. More…
| Header | #include <QAbstractAnimation> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 4.6 |
| Inherits | QObject |
| Inherited by | QAnimationGroup, QPauseAnimation, QVariantAnimation |
List of all members, including inherited members
Public Types
| enum | Direction |
| enum | State |
| enum | DeletionPolicy |
Public Functions
| QAbstractAnimation(QObject *parent = nullptr) | |
| ~QAbstractAnimation() | |
| State | state() const |
| QAnimationGroup * | group() const |
| Direction | direction() const |
| void | setDirection(Direction direction) |
| int | currentTime() const |
| int | currentLoopTime() const |
| int | loopCount() const |
| void | setLoopCount(int loopCount) |
| int | currentLoop() const |
| virtual int | duration() const |
| int | totalDuration() const |
Public Slots
| void | start(QAbstractAnimation::DeletionPolicy policy = KeepWhenStopped) |
| void | pause() |
| void | resume() |
| void | setPaused(bool paused) |
| void | stop() |
| void | setCurrentTime(int msecs) |
Signals
| void | finished() |
| void | stateChanged(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) |
| void | currentLoopChanged(int currentLoop) |
| void | directionChanged(QAbstractAnimation::Direction newDirection) |
Protected Functions
| virtual void | updateCurrentTime(int currentTime) |
| virtual void | updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) |
| virtual void | updateDirection(QAbstractAnimation::Direction direction) |
Reimplemented Protected Functions
Detailed Description
An animation is something that changes over a time: it has a duration(), a current time between 0 and the duration, and can be started, paused, resumed and stopped. While it runs, the event loop of its thread moves its current time forward about sixty times a second, and each time the animation is told with updateCurrentTime().
The classes that are used most are QPropertyAnimation, which changes a property of an object, and the groups QSequentialAnimationGroup and QParallelAnimationGroup, which run animations one after another or together. To make an animation of another kind, subclass QAbstractAnimation and implement duration() and updateCurrentTime():
class Blink : public QAbstractAnimation {
public:
int duration() const override { return 1000; }
protected:
void updateCurrentTime(int time) override { setLamp(time < 500); }
};
An animation can run several times, see setLoopCount(), and backwards, see setDirection(). When it reaches its end it stops and emits finished().
See also QVariantAnimation, QPropertyAnimation and QAnimationGroup.
Differences from Qt
The properties are not bindable. Time advances with a timer of the event loop of the thread that the animation is started in, and there is no QAnimationDriver to replace that timer.
Member Type Documentation
enum QAbstractAnimation::DeletionPolicy
What start() arranges for the time the animation stops.
| Constant | Description |
|---|---|
QAbstractAnimation::KeepWhenStopped | The animation stays, to be started again. |
QAbstractAnimation::DeleteWhenStopped | The animation is deleted when it stops, with deleteLater(). |
enum QAbstractAnimation::Direction
The direction in which the current time moves while the animation runs.
| Constant | Description |
|---|---|
QAbstractAnimation::Forward | The current time increases, from 0 to the end. |
QAbstractAnimation::Backward | The current time decreases, from the end to 0. |
enum QAbstractAnimation::State
The states of an animation.
| Constant | Description |
|---|---|
QAbstractAnimation::Stopped | The animation does not run. Starting it begins at the start, or for the direction Backward at the end. |
QAbstractAnimation::Paused | The animation is held where it is. resume() lets it go on from there. |
QAbstractAnimation::Running | The animation runs: the event loop moves its current time. |
Member Function Documentation
QAbstractAnimation::QAbstractAnimation(QObject *parent = nullptr)
Constructs an animation with the given parent. If the parent is a QAnimationGroup, add the animation to it with QAnimationGroup::addAnimation().
QAbstractAnimation::~QAbstractAnimation()
Destroys the animation, after stopping it and taking it out of its group.
int QAbstractAnimation::currentLoop() const
Returns the number of the loop that the animation is in, starting with 0.
See also loopCount() and currentLoopChanged().
void QAbstractAnimation::currentLoopChanged(int currentLoop)
This signal is emitted when the animation starts another loop, with the number of the loop in currentLoop.
See also currentLoop() and setLoopCount().
int QAbstractAnimation::currentLoopTime() const
Returns the current time inside the current loop, from 0 to duration().
See also currentTime() and currentLoop().
int QAbstractAnimation::currentTime() const
Returns the current time of the animation in milliseconds, counted over all its loops: from 0 to totalDuration().
See also setCurrentTime() and currentLoopTime().
Direction QAbstractAnimation::direction() const
Returns the direction of the animation, which is Forward by default.
See also setDirection().
void QAbstractAnimation::directionChanged(QAbstractAnimation::Direction newDirection)
This signal is emitted when the direction has changed, with the new direction in newDirection.
See also setDirection().
int QAbstractAnimation::duration() const
Returns the duration of one loop of the animation in milliseconds. -1 means that the duration is undefined: the animation runs until it stops itself or is stopped.
See also totalDuration() and loopCount().
bool QAbstractAnimation::event(QEvent *event)
Reimplements QObject::event().
void QAbstractAnimation::finished()
This signal is emitted when the animation has run to its end and stopped by itself. It is not emitted when stop() ends an animation early, unless the animation has no end of its own: an undefined duration or loops without end.
See also stateChanged() and stop().
QAnimationGroup * QAbstractAnimation::group() const
Returns the group that the animation is in, or nullptr if it is in none. An animation in a group is started, paused and stopped by its group.
See also QAnimationGroup::addAnimation().
int QAbstractAnimation::loopCount() const
Returns the number of times the animation runs before it stops, which is 1 by default. -1 means that it runs until it is stopped.
See also setLoopCount().
void QAbstractAnimation::pause()
Pauses a running animation: the current time stays where it is until resume() or stop().
See also resume() and setPaused().
void QAbstractAnimation::resume()
Lets a paused animation go on from where it was paused.
See also pause() and setPaused().
void QAbstractAnimation::setCurrentTime(int msecs)
Sets the current time of the animation to msecs, a time between 0 and totalDuration(), and updates the animation to that time. The event loop calls this for an animation that runs; a program can call it to move an animation by hand, also one that is paused or stopped. An animation that runs and is put at its end stops.
See also currentTime() and updateCurrentTime().
void QAbstractAnimation::setDirection(Direction direction)
Sets the direction of the animation to direction. An animation that runs goes on from where it is in the new direction. A stopped animation with the direction Backward starts at its end; if it loops without end, it starts at the end of its first loop.
See also direction() and directionChanged().
void QAbstractAnimation::setLoopCount(int loopCount)
Sets the number of times the animation runs to loopCount. With 0 the animation does not run at all, and with -1 it runs until it is stopped.
See also loopCount() and currentLoop().
void QAbstractAnimation::setPaused(bool paused)
Pauses the animation if paused is true, and resumes it otherwise.
See also pause() and resume().
void QAbstractAnimation::start(QAbstractAnimation::DeletionPolicy policy = KeepWhenStopped)
Starts the animation: the state becomes Running, and the event loop moves the current time from the start to the end. With the policy DeleteWhenStopped the animation is deleted when it stops. Starting an animation that runs does nothing; a paused one starts over from the beginning only after stop(), use resume() to go on.
The animation needs the event loop of its thread, as every timer does.
See also stop(), pause() and finished().
State QAbstractAnimation::state() const
Returns the state of the animation.
See also start(), pause(), stop() and stateChanged().
void QAbstractAnimation::stateChanged(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
This signal is emitted when the state of the animation has changed from oldState to newState.
See also updateState().
void QAbstractAnimation::stop()
Stops the animation. The current time stays where it is, and the next start() begins at the start again. finished() is not emitted, unless the animation has no end of its own.
See also start() and finished().
int QAbstractAnimation::totalDuration() const
Returns the duration of all loops together: duration() times loopCount(). It is -1 if the duration is undefined or the animation loops without end.
See also duration() and currentTime().
void QAbstractAnimation::updateCurrentTime(int currentTime)
Called when the current time of the animation has changed, with the time inside the current loop in currentTime. A subclass shows the animation as it is at that time.
See also setCurrentTime() and updateState().
void QAbstractAnimation::updateDirection(QAbstractAnimation::Direction direction)
Called when the direction of the animation changes to direction, before directionChanged() is emitted. The default implementation does nothing.
See also setDirection().
void QAbstractAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
Called when the state of the animation changes from oldState to newState, before stateChanged() is emitted. The default implementation does nothing.