QTimeLine Class

The QTimeLine class provides a timeline for controlling animations. More...

Header: #include <QTimeLine>
qmake: QT += core
Since: Qt 4.2
Inherits: QObject

This class was introduced in Qt 4.2.

Public Types

enum Direction { Forward, Backward }
enum State { NotRunning, Paused, Running }

Properties

Public Functions

int currentTime() const
QTimeLine::Direction direction() const
int duration() const
QEasingCurve easingCurve() const
int loopCount() const
void setDirection(QTimeLine::Direction direction)
void setDuration(int duration)
void setEasingCurve(const QEasingCurve &curve)
void setLoopCount(int count)
void setUpdateInterval(int interval)
int updateInterval() const

Public Slots

void setCurrentTime(int msec)

Detailed Description

It's most commonly used to animate a GUI control by calling a slot periodically. You can construct a timeline by passing its duration in milliseconds to QTimeLine's constructor. The timeline's duration describes for how long the animation will run. Then you set a suitable frame range by calling setFrameRange(). Finally connect the frameChanged() signal to a suitable slot in the widget you wish to animate (for example, setValue() in QProgressBar). When you proceed to calling start(), QTimeLine will enter Running state, and start emitting frameChanged() at regular intervals, causing your widget's connected property's value to grow from the lower end to the upper and of your frame range, at a steady rate. You can specify the update interval by calling setUpdateInterval(). When done, QTimeLine enters NotRunning state, and emits finished().

Example:

 ...
 progressBar = new QProgressBar(this);
 progressBar->setRange(0, 100);

 // Construct a 1-second timeline with a frame range of 0 - 100
 QTimeLine *timeLine = new QTimeLine(1000, this);
 timeLine->setFrameRange(0, 100);
 connect(timeLine, &QTimeLine::frameChanged, progressBar, &QProgressBar::setValue);

 // Clicking the push button will start the progress bar animation
 pushButton = new QPushButton(tr("Start animation"), this);
 connect(pushButton, &QPushButton::clicked, timeLine, &QTimeLine::start);
 ...

By default the timeline runs once, from its beginning to its end, upon which you must call start() again to restart from the beginning. To make the timeline loop, you can call setLoopCount(), passing the number of times the timeline should run before finishing. The direction can also be changed, causing the timeline to run backward, by calling setDirection(). You can also pause and unpause the timeline while it's running by calling setPaused(). For interactive control, the setCurrentTime() function is provided, which sets the time position of the time line directly. Although most useful in NotRunning state (e.g., connected to a valueChanged() signal in a QSlider), this function can be called at any time.

The frame interface is useful for standard widgets, but QTimeLine can be used to control any type of animation. The heart of QTimeLine lies in the valueForTime() function, which generates a value between 0 and 1 for a given time. This value is typically used to describe the steps of an animation, where 0 is the first step of an animation, and 1 is the last step. When running, QTimeLine generates values between 0 and 1 by calling valueForTime() and emitting valueChanged(). By default, valueForTime() applies an interpolation algorithm to generate these value. You can choose from a set of predefined timeline algorithms by calling setEasingCurve().

Note that, by default, QTimeLine uses QEasingCurve::InOutSine, which provides a value that grows slowly, then grows steadily, and finally grows slowly. For a custom timeline, you can reimplement valueForTime(), in which case QTimeLine's easingCurve property is ignored.

See also QProgressBar and QProgressDialog.

Member Type Documentation

enum QTimeLine::Direction

This enum describes the direction of the timeline when in Running state.

ConstantValueDescription
QTimeLine::Forward0The current time of the timeline increases with time (i.e., moves from 0 and towards the end / duration).
QTimeLine::Backward1The current time of the timeline decreases with time (i.e., moves from the end / duration and towards 0).

See also setDirection().

enum QTimeLine::State

This enum describes the state of the timeline.

ConstantValueDescription
QTimeLine::NotRunning0The timeline is not running. This is the initial state of QTimeLine, and the state QTimeLine reenters when finished. The current time, frame and value remain unchanged until either setCurrentTime() is called, or the timeline is started by calling start().
QTimeLine::Paused1The timeline is paused (i.e., temporarily suspended). Calling setPaused(false) will resume timeline activity.
QTimeLine::Running2The timeline is running. While control is in the event loop, QTimeLine will update its current time at regular intervals, emitting valueChanged() and frameChanged() when appropriate.

See also state() and stateChanged().

Property Documentation

currentTime : int

This property holds the current time of the time line.

When QTimeLine is in Running state, this value is updated continuously as a function of the duration and direction of the timeline. Otherwise, it is value that was current when stop() was called last, or the value set by setCurrentTime().

By default, this property contains a value of 0.

Access functions:

int currentTime() const
void setCurrentTime(int msec)

direction : Direction

This property holds the direction of the timeline when QTimeLine is in Running state.

This direction indicates whether the time moves from 0 towards the timeline duration, or from the value of the duration and towards 0 after start() has been called.

By default, this property is set to Forward.

Access functions:

QTimeLine::Direction direction() const
void setDirection(QTimeLine::Direction direction)

duration : int

This property holds the total duration of the timeline in milliseconds.

By default, this value is 1000 (i.e., 1 second), but you can change this by either passing a duration to QTimeLine's constructor, or by calling setDuration(). The duration must be larger than 0.

Note: Changing the duration does not cause the current time to be reset to zero or the new duration. You also need to call setCurrentTime() with the desired value.

Access functions:

int duration() const
void setDuration(int duration)

easingCurve : QEasingCurve

Specifies the easing curve that the timeline will use. If valueForTime() is reimplemented, this value is ignored. If both easingCurve and curveShape are set, the last property set will override the previous one.

This property was introduced in Qt 4.6.

Access functions:

QEasingCurve easingCurve() const
void setEasingCurve(const QEasingCurve &curve)

See also valueForTime().

loopCount : int

This property holds the number of times the timeline should loop before it's finished.

A loop count of of 0 means that the timeline will loop forever.

By default, this property contains a value of 1.

Access functions:

int loopCount() const
void setLoopCount(int count)

updateInterval : int

This property holds the time in milliseconds between each time QTimeLine updates its current time.

When updating the current time, QTimeLine will emit valueChanged() if the current value changed, and frameChanged() if the frame changed.

By default, the interval is 40 ms, which corresponds to a rate of 25 updates per second.

Access functions:

int updateInterval() const
void setUpdateInterval(int interval)