![]() |
Home · Overviews · Examples |
The QTimeLine class provides a timeline for controlling animations. More...
Inherits QObject.
The QTimeLine 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 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 (e.g., 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, SIGNAL(frameChanged(int)), progressBar, SLOT(setValue(int))); // Clicking the push button will start the progress bar animation pushButton = new QPushButton(tr("Start animation"), this); connect(pushButton, SIGNAL(clicked()), timeLine, SLOT(start())); ...
You can also use QTimeLine with the Graphics View framework for animations. The QGraphicsItemAnimation class implements animation of QGraphicsItems with a timeline.
By default the timeline runs once, from the beginning and towards the 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 setCurveShape. 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 reimplement valueForTime, in which case QTimeLine's curveShape property is ignored.
See also QProgressBar, QProgressDialog, and QGraphicsItemAnimation.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.3.5_01 |