QTimeLine Class Reference

[ QtCore module]

The QTimeLine class provides a timeline for controlling animations. 更多...

继承 QObject .

类型

方法

Qt Signals


详细描述

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 ().

范例:

 ...
 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 到 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 () 和 emitting valueChanged (). 默认情况下, valueForTime () applies an interpolation algorithm to generate these value. You can choose from a set of predefined timeline algorithms by calling setCurveShape ().

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 reimplement valueForTime (), in which case QTimeLine's curveShape 特性为 ignored.


类型文档编制

QTimeLine.CurveShape

This enum describes the default shape of QTimeLine 's value curve. The default, shape is EaseInOutCurve. The curve defines the relation between the value and the timeline.

常量 描述
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.

另请参阅 setCurveShape ().

QTimeLine.Direction

This enum describes the direction of the timeline when in 运行 状态。

常量 描述
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).

另请参阅 setDirection ().

QTimeLine.State

This enum describes the state of the timeline.

常量 描述
QTimeLine.NotRunning 0 The timeline is not running. This is the initial state of QTimeLine ,和 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.Paused 1 The timeline is paused (i.e., temporarily suspended). Calling setPaused(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, emitting valueChanged () 和 frameChanged () when appropriate.

另请参阅 state () 和 stateChanged ().


方法文档编制

QTimeLine.__init__ ( self , int  duration  = 1000, QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a timeline with a duration of duration 毫秒。 parent 会被传递给 QObject 's constructor. The default duration is 1000 milliseconds.

int QTimeLine.currentFrame ( self )

Returns the frame corresponding to the current time.

另请参阅 currentTime (), frameForTime (),和 setFrameRange ().

int QTimeLine.currentTime ( self )

float QTimeLine.currentValue ( self )

Returns the value corresponding to the current time.

另请参阅 valueForTime () 和 currentFrame ().

CurveShape QTimeLine.curveShape ( self )

Direction QTimeLine.direction ( self )

int QTimeLine.duration ( self )

QEasingCurve QTimeLine.easingCurve ( self )

int QTimeLine.endFrame ( self )

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).

另请参阅 setEndFrame () 和 setFrameRange ().

int QTimeLine.frameForTime ( self , int  msec )

Returns the frame corresponding to the time msec . This value is calculated using a linear interpolation of the start and end frame, based on the value returned by valueForTime ().

另请参阅 valueForTime () 和 setFrameRange ().

int QTimeLine.loopCount ( self )

QTimeLine.resume ( self )

This method is also a Qt slot with the C++ signature void 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 to start (), this function does not restart the timeline before it resumes.

另请参阅 start (), updateInterval (), frameChanged (),和 valueChanged ().

QTimeLine.setCurrentTime ( self , int  msec )

This method is also a Qt slot with the C++ signature void setCurrentTime(int) .

QTimeLine.setCurveShape ( self , CurveShape   shape )

QTimeLine.setDirection ( self , Direction   direction )

QTimeLine.setDuration ( self , int  duration )

QTimeLine.setEasingCurve ( self , QEasingCurve   curve )

QTimeLine.setEndFrame ( self , int  frame )

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), to frame .

另请参阅 endFrame (), startFrame (),和 setFrameRange ().

QTimeLine.setFrameRange ( self , int  startFrame , int  endFrame )

Sets the timeline's frame counter to start at startFrame , and end and endFrame . For each time value, QTimeLine will find the corresponding frame when you call currentFrame () 或 frameForTime () by interpolating, using the return value of valueForTime ().

When in Running state, QTimeLine also emits the frameChanged () signal when the frame changes.

另请参阅 startFrame (), endFrame (), start (),和 currentFrame ().

QTimeLine.setLoopCount ( self , int  count )

QTimeLine.setPaused ( self , bool  paused )

This method is also a Qt slot with the C++ signature void setPaused(bool) .

paused is true, the timeline is paused, causing QTimeLine to enter Paused state. No updates will be signaled until either start () or setPaused(false) is called. 若 paused is false, the timeline is resumed and continues where it left.

另请参阅 state () 和 start ().

QTimeLine.setStartFrame ( self , int  frame )

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), to frame .

另请参阅 startFrame (), endFrame (),和 setFrameRange ().

QTimeLine.setUpdateInterval ( self , int  interval )

QTimeLine.start ( self )

This method is also a Qt slot with the C++ signature void 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 calling setUpdateInterval ().

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 call resume () 代替。

另请参阅 resume (), updateInterval (), frameChanged (),和 valueChanged ().

int QTimeLine.startFrame ( self )

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).

另请参阅 setStartFrame () 和 setFrameRange ().

State QTimeLine.state ( self )

Returns the state of the timeline.

另请参阅 start (), setPaused (),和 stop ().

QTimeLine.stop ( self )

This method is also a Qt slot with the C++ signature void stop() .

Stops the timeline, causing QTimeLine to enter NotRunning 状态。

另请参阅 start ().

QTimeLine.timerEvent ( self , QTimerEvent   event )

重实现自 QObject.timerEvent ().

QTimeLine.toggleDirection ( self )

This method is also a Qt slot with the C++ signature void toggleDirection() .

Toggles the direction of the timeline. If the direction was Forward, it becomes Backward, and vice verca.

另请参阅 setDirection ().

int QTimeLine.updateInterval ( self )

float QTimeLine.valueForTime ( self , int  msec )

Returns the timeline value for the time msec 。 returned value, which varies depending on the curve shape, is always between 0 and 1. If msec is 0, the default implementation always returns 0.

Reimplement this function to provide a custom curve shape for your timeline.

另请参阅 CurveShape and frameForTime ().


Qt Signal Documentation

void finished ()

This is the default overload of this signal.

此信号被发射当 QTimeLine finishes (i.e., reaches the end of its time line), and does not loop.

void frameChanged (int)

This is the default overload of this signal.

QTimeLine emits this signal at regular intervals when in 运行 state, but only if the current frame changes. frame is the current frame number.

另请参阅 QTimeLine.setFrameRange () 和 QTimeLine.updateInterval .

void stateChanged (QTimeLine::State)

This is the default overload of this signal.

此信号被发射每当 QTimeLine 's state changes. The new state is newState .

void valueChanged (qreal)

This is the default overload of this signal.

QTimeLine emits this signal at regular intervals when in 运行 state, but only if the current value changes. value is the current value. value is a number between 0.0 and 1.0

另请参阅 QTimeLine.setDuration (), QTimeLine.valueForTime (),和 QTimeLine.updateInterval .