
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQTimeLine class provides a timeline for controlling animations.More...
| Header: | #include <QTimeLine> |
| Since: | Qt 4.2 |
| Inherits: | QObject |
| enum | CurveShape { EaseInCurve, EaseOutCurve, EaseInOutCurve, LinearCurve, SineCurve, CosineCurve } |
| enum | Direction { Forward, Backward } |
| enum | State { NotRunning, Paused, Running } |
|
|
| QTimeLine(int duration = 1000, QObject * parent = 0) | |
| virtual | ~QTimeLine() |
| int | currentFrame() const |
| int | currentTime() const |
| qreal | currentValue() const |
| CurveShape | curveShape() const |
| Direction | direction() const |
| int | duration() const |
| QEasingCurve | easingCurve() const |
| int | endFrame() const |
| int | frameForTime(int msec) const |
| int | loopCount() const |
| void | setCurveShape(CurveShape shape) |
| void | setDirection(Direction direction) |
| void | setDuration(int duration) |
| void | setEasingCurve(const QEasingCurve & curve) |
| void | setEndFrame(int frame) |
| void | setFrameRange(int startFrame, int endFrame) |
| void | setLoopCount(int count) |
| void | setStartFrame(int frame) |
| void | setUpdateInterval(int interval) |
| int | startFrame() const |
| State | state() const |
| int | updateInterval() const |
| virtual qreal | valueForTime(int msec) const |
| void | resume() |
| void | setCurrentTime(int msec) |
| void | setPaused(bool paused) |
| void | start() |
| void | stop() |
| void | toggleDirection() |
| void | finished() |
| void | frameChanged(int frame) |
| void | stateChanged(QTimeLine::State newState) |
| void | valueChanged(qreal value) |
| virtual void | timerEvent(QTimerEvent * event) |
TheQTimeLine class provides a timeline for controlling animations.
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 toQTimeLine's constructor. The timeline's duration describes for how long the animation will run. Then you set a suitable frame range by callingsetFrameRange(). Finally connect theframeChanged() signal to a suitable slot in the widget you wish to animate (e.g., setValue() inQProgressBar). When you proceed to callingstart(),QTimeLine will enter Running state, and start emittingframeChanged() 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 callingsetUpdateInterval(). When done,QTimeLine entersNotRunning state, and emitsfinished().
Example:
...progressBar=newQProgressBar(this);progressBar->setRange(0,100);// Construct a 1-second timeline with a frame range of 0 - 100QTimeLine*timeLine=newQTimeLine(1000,this);timeLine->setFrameRange(0,100);connect(timeLine, SIGNAL(frameChanged(int)), progressBar, SLOT(setValue(int)));// Clicking the push button will start the progress bar animationpushButton=newQPushButton(tr("Start animation"),this);connect(pushButton, SIGNAL(clicked()), timeLine, SLOT(start()));...
You can also useQTimeLine with theGraphics View framework for animations. The QGraphicsItemAnimation class implements animation ofQGraphicsItems with a timeline.
By default the timeline runs once, from the beginning and towards the end, upon which you must callstart() again to restart from the beginning. To make the timeline loop, you can callsetLoopCount(), passing the number of times the timeline should run before finishing. The direction can also be changed, causing the timeline to run backward, by callingsetDirection(). You can also pause and unpause the timeline while it's running by callingsetPaused(). For interactive control, thesetCurrentTime() function is provided, which sets the time position of the time line directly. Although most useful inNotRunning state, (e.g., connected to avalueChanged() signal in aQSlider,) this function can be called at any time.
The frame interface is useful for standard widgets, butQTimeLine can be used to control any type of animation. The heart ofQTimeLine lies in thevalueForTime() function, which generates avalue 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 callingvalueForTime() and emittingvalueChanged(). By default,valueForTime() applies an interpolation algorithm to generate these value. You can choose from a set of predefined timeline algorithms by callingsetCurveShape().
Note that by default,QTimeLine uses the EaseInOut curve shape, which provides a value that grows slowly, then grows steadily, and finally grows slowly. For a custom timeline, you can reimplementvalueForTime(), in which caseQTimeLine'scurveShape property is ignored.
See alsoQProgressBar,QProgressDialog, andQGraphicsItemAnimation.
This enum describes the default shape ofQTimeLine's value curve. The default, shape is EaseInOutCurve. The curve defines the relation between the value and the timeline.
| Constant | Value | Description |
|---|---|---|
QTimeLine::EaseInCurve | 0 | The value starts growing slowly, then increases in speed. |
QTimeLine::EaseOutCurve | 1 | The value starts growing steadily, then ends slowly. |
QTimeLine::EaseInOutCurve | 2 | The value starts growing slowly, then runs steadily, then grows slowly again. |
QTimeLine::LinearCurve | 3 | The value grows linearly (e.g., if the duration is 1000 ms, the value at time 500 ms is 0.5). |
QTimeLine::SineCurve | 4 | The value grows sinusoidally. |
QTimeLine::CosineCurve | 5 | The value grows cosinusoidally. |
See alsosetCurveShape().
This enum describes the direction of the timeline when inRunning state.
| Constant | Value | Description |
|---|---|---|
QTimeLine::Forward | 0 | The current time of the timeline increases with time (i.e., moves from 0 and towards the end / duration). |
QTimeLine::Backward | 1 | The current time of the timeline decreases with time (i.e., moves from the end / duration and towards 0). |
See alsosetDirection().
This enum describes the state of the timeline.
| Constant | Value | Description |
|---|---|---|
QTimeLine::NotRunning | 0 | The timeline is not running. This is the initial state ofQTimeLine, and the stateQTimeLine reenters when finished. The current time, frame and value remain unchanged until eithersetCurrentTime() is called, or the timeline is started by callingstart(). |
QTimeLine::Paused | 1 | The timeline is paused (i.e., temporarily suspended). CallingsetPaused(false) will resume timeline activity. |
QTimeLine::Running | 2 | The timeline is running. While control is in the event loop,QTimeLine will update its current time at regular intervals, emittingvalueChanged() andframeChanged() when appropriate. |
See alsostate() andstateChanged().
This property holds the current time of the time line.
WhenQTimeLine 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 whenstop() 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) |
This property holds the shape of the timeline curve.
The curve shape describes the relation between the time and value for the base implementation ofvalueForTime().
If you have reimplementedvalueForTime(), this value is ignored.
By default, this property is set toEaseInOutCurve.
Access functions:
| CurveShape | curveShape() const |
| void | setCurveShape(CurveShape shape) |
See alsovalueForTime().
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 afterstart() has been called.
By default, this property is set toForward.
Access functions:
| Direction | direction() const |
| void | setDirection(Direction direction) |
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 toQTimeLine'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 callsetCurrentTime() with the desired value.
Access functions:
| int | duration() const |
| void | setDuration(int duration) |
Specifies the easing curve that the timeline will use. If both easing curve andcurveShape are set, the last set property will override the previous one. (IfvalueForTime() is reimplemented it will override both)
This property was introduced in Qt 4.6.
Access functions:
| QEasingCurve | easingCurve() const |
| void | setEasingCurve(const QEasingCurve & curve) |
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) |
This property holds the time in milliseconds between each time QTimeLine updates its current time.
When updating the current time,QTimeLine will emitvalueChanged() if the current value changed, andframeChanged() 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) |
Constructs a timeline with a duration ofduration milliseconds.parent is passed toQObject's constructor. The default duration is 1000 milliseconds.
[virtual]QTimeLine::~QTimeLine()Destroys the timeline.
Returns the frame corresponding to the current time.
See alsocurrentTime(),frameForTime(), andsetFrameRange().
Returns the value corresponding to the current time.
See alsovalueForTime() andcurrentFrame().
Returns the end frame, which is the frame corresponding to the end of the timeline (i.e., the frame for which the current value is 1).
See alsosetEndFrame() andsetFrameRange().
[signal]void QTimeLine::finished()This signal is emitted whenQTimeLine finishes (i.e., reaches the end of its time line), and does not loop.
[signal]void QTimeLine::frameChanged(int frame)QTimeLine emits this signal at regular intervals when inRunning state, but only if the current frame changes.frame is the current frame number.
See alsoQTimeLine::setFrameRange() andQTimeLine::updateInterval.
Returns the frame corresponding to the timemsec. This value is calculated using a linear interpolation of the start and end frame, based on the value returned byvalueForTime().
See alsovalueForTime() andsetFrameRange().
[slot]void QTimeLine::resume()Resumes the timeline from the current time.QTimeLine will reenter Running state, and once it enters the event loop, it will update its current time, frame and value at regular intervals.
In contrast tostart(), this function does not restart the timeline before it resumes.
See alsostart(),updateInterval(),frameChanged(), andvalueChanged().
Sets the end frame, which is the frame corresponding to the end of the timeline (i.e., the frame for which the current value is 1), toframe.
See alsoendFrame(),startFrame(), andsetFrameRange().
Sets the timeline's frame counter to start atstartFrame, and end andendFrame. For each time value,QTimeLine will find the corresponding frame when you callcurrentFrame() orframeForTime() by interpolating, using the return value ofvalueForTime().
When in Running state,QTimeLine also emits theframeChanged() signal when the frame changes.
See alsostartFrame(),endFrame(),start(), andcurrentFrame().
[slot]void QTimeLine::setPaused(bool paused)Ifpaused is true, the timeline is paused, causingQTimeLine to enter Paused state. No updates will be signaled until eitherstart() or setPaused(false) is called. Ifpaused is false, the timeline is resumed and continues where it left.
Sets the start frame, which is the frame corresponding to the start of the timeline (i.e., the frame for which the current value is 0), toframe.
See alsostartFrame(),endFrame(), andsetFrameRange().
[slot]void QTimeLine::start()Starts the timeline.QTimeLine will enter Running state, and once it enters the event loop, it will update its current time, frame and value at regular intervals. The default interval is 40 ms (i.e., 25 times per second). You can change the update interval by callingsetUpdateInterval().
The timeline will start from position 0, or the end if going backward. If you want to resume a stopped timeline without restarting, you can callresume() instead.
See alsoresume(),updateInterval(),frameChanged(), andvalueChanged().
Returns the start frame, which is the frame corresponding to the start of the timeline (i.e., the frame for which the current value is 0).
See alsosetStartFrame() andsetFrameRange().
Returns the state of the timeline.
See alsostart(),setPaused(), andstop().
[signal]void QTimeLine::stateChanged(QTimeLine::State newState)This signal is emitted wheneverQTimeLine's state changes. The new state isnewState.
[slot]void QTimeLine::stop()Stops the timeline, causingQTimeLine to enterNotRunning state.
See alsostart().
[virtual protected]void QTimeLine::timerEvent(QTimerEvent * event)Reimplemented fromQObject::timerEvent().
[slot]void QTimeLine::toggleDirection()Toggles the direction of the timeline. If the direction was Forward, it becomes Backward, and vice verca.
See alsosetDirection().
[signal]void QTimeLine::valueChanged(qreal value)QTimeLine emits this signal at regular intervals when inRunning state, but only if the current value changes.value is the current value.value is a number between 0.0 and 1.0
See alsoQTimeLine::setDuration(),QTimeLine::valueForTime(), andQTimeLine::updateInterval.
[virtual]qreal QTimeLine::valueForTime(int msec) constReturns the timeline value for the timemsec. The returned value, which varies depending on the curve shape, is always between 0 and 1. Ifmsec is 0, the default implementation always returns 0.
Reimplement this function to provide a custom curve shape for your timeline.
See alsoCurveShape andframeForTime().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.