QTimeLine Class
QTimeLine 提供用于控制动画的时间线. More...
头文件: | #include <QTimeLine> |
qmake: | QT += core |
开始支持版本: | Qt 4.2 |
基类: | QObject |
公有类型
属性
|
|
- 1 个属性继承自 QObject
公有函数
QTimeLine(int duration = 1000, QObject *parent = nullptr) | |
virtual | ~QTimeLine() |
int | currentFrame() const |
int | currentTime() const |
qreal | currentValue() const |
QTimeLine::Direction | direction() const |
int | duration() const |
QEasingCurve | easingCurve() const |
int | endFrame() const |
int | frameForTime(int msec) const |
int | loopCount() const |
void | setDirection(QTimeLine::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 |
QTimeLine::State | state() const |
int | updateInterval() const |
virtual qreal | valueForTime(int msec) const |
- 32 个公有函数继承自 QObject
公有槽函数
void | resume() |
void | setCurrentTime(int msec) |
void | setPaused(bool paused) |
void | start() |
void | stop() |
void | toggleDirection() |
- 1 个公有槽函数继承自 QObject
信号
void | finished() |
void | frameChanged(int frame) |
void | stateChanged(QTimeLine::State newState) |
void | valueChanged(qreal value) |
- 2 个信号继承自 QObject
重新实现的受保护函数
virtual void | timerEvent(QTimerEvent *event) |
- 9 个受保护的函数继承自 QObject
其他继承的成员
详细描述
QTimeLine 通常通过周期性地调用槽函数, 为 GUI 控件设置动画效果. 你可以通过以毫秒为单位为 QTimeline 的构造函数来构建时间线. 时间轴的持续时间描述了动画将运行多长时间. 然后通过调用 setFrameRange() 设置合适的帧范围. 最后将 frameChanged() 信号连接到你希望动画的窗口widget中的合适槽函数 (例如, QProgressBar 中的 setValue()). 当你继续调用 start() 时, QTimeLine 将进入 Running 状态, 并开始定期发送 frameChanged() 信号, 使你的widget连接的属性值从较低端增长到较高端, 并以一个稳定的速率增长到帧范围的较高端. 你可以调用 setUpdateInterval() 设置更新间隔. 完成后, QTimeLine 进入 NotRunning 状态, 并发出 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, &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); ...
默认情况下, 时间线从开始到结束运行一次, 你必须在此时间线上再次调用 start() 以重新启动. 要使时间线循环, 可以调用 setLoopCount(), 传递在完成之前时间线应该运行的次数. 还可以通过调用 setDirection() 更改方向, 从而使时间线向后运行. 你还可以在时间轴运行时调用 setPaused() 暂停和取消暂停. 对于交互控制, 提供了 setCurrentTime() 函数, 它直接设置时间线的时间位置. 尽管这个函数在 NotRunning 状态下最有用 (例如., 连接到 QSlider 中的 valueChanged() 信号), 但是这个函数可以在任何时候调用.
框架界面对于标准部件很有用, 但 QTimeLine 可以用来控制任何类型的动画. QTimeLine 的核心在 valueForTime() 函数中, 该函数在给定时间内生成介于0和1之间的值. 此值通常用于描述动画的步骤, 其中 0 是动画的第一步, 1 是最后一步. 在运行时, QTimeLine 调用 valueForTime() 生成0到1之间的值, 并发出 valueChanged() 信号. 默认情况下, valueForTime() 应用插值算法来生成这些值. 你可以调用 setEasingCurve() 从一组预定义的时间线算法中进行选择.
注意, 默认情况下, QTimeLine 使用 QEasingCurve::InOutSine, 它提供一个缓慢增长的值, 然后稳定增长, 最后缓慢增长. 对于自定义时间线, 你可以重新实现 valueForTime(), 在这种情况下, 忽略 QTimeLine 的 easingCurve 属性.
另见 QProgressBar, QProgressDialog.
Member Type Documentation
enum QTimeLine::Direction
This enum describes the direction of the timeline when in Running 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 also setDirection().
enum QTimeLine::State
This enum describes the state of the timeline.
Constant | Value | Description |
---|---|---|
QTimeLine::NotRunning | 0 | The 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::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() 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) |
Member Function Documentation
QTimeLine::QTimeLine(int duration = 1000, QObject *parent = nullptr)
Constructs a timeline with a duration of duration milliseconds. parent is passed to QObject's constructor. The default duration is 1000 milliseconds.
[signal]
void QTimeLine::finished()
This signal is emitted when QTimeLine finishes (i.e., reaches the end of its time line), and does not loop.
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
[signal]
void QTimeLine::frameChanged(int frame)
QTimeLine emits this signal at regular intervals when in Running state, but only if the current frame changes. frame is the current frame number.
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
See also QTimeLine::setFrameRange() and QTimeLine::updateInterval.
[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 to start(), this function does not restart the timeline before it resumes.
See also start(), updateInterval(), frameChanged(), and valueChanged().
[slot]
void QTimeLine::setPaused(bool paused)
If 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. If paused is false, the timeline is resumed and continues where it left.
[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 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() instead.
See also resume(), updateInterval(), frameChanged(), and valueChanged().
[signal]
void QTimeLine::stateChanged(QTimeLine::State newState)
This signal is emitted whenever QTimeLine's state changes. The new state is newState.
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
[slot]
void QTimeLine::stop()
Stops the timeline, causing QTimeLine to enter NotRunning state.
See also start().
[slot]
void QTimeLine::toggleDirection()
Toggles the direction of the timeline. If the direction was Forward, it becomes Backward, and vice verca.
See also setDirection().
[signal]
void QTimeLine::valueChanged(qreal value)
QTimeLine emits this signal at regular intervals when in Running state, but only if the current value changes. value is the current value. value is a number between 0.0 and 1.0
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
See also QTimeLine::setDuration(), QTimeLine::valueForTime(), and QTimeLine::updateInterval.
[virtual]
QTimeLine::~QTimeLine()
Destroys the timeline.
int QTimeLine::currentFrame() const
Returns the frame corresponding to the current time.
See also currentTime(), frameForTime(), and setFrameRange().
qreal QTimeLine::currentValue() const
Returns the value corresponding to the current time.
See also valueForTime() and currentFrame().
int QTimeLine::endFrame() const
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 also setEndFrame() and setFrameRange().
int QTimeLine::frameForTime(int msec) const
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().
See also valueForTime() and setFrameRange().
void QTimeLine::setEndFrame(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.
See also endFrame(), startFrame(), and setFrameRange().
void QTimeLine::setFrameRange(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() or frameForTime() by interpolating, using the return value of valueForTime().
When in Running state, QTimeLine also emits the frameChanged() signal when the frame changes.
See also startFrame(), endFrame(), start(), and currentFrame().
void QTimeLine::setStartFrame(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.
See also startFrame(), endFrame(), and setFrameRange().
int QTimeLine::startFrame() const
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 also setStartFrame() and setFrameRange().
QTimeLine::State QTimeLine::state() const
Returns the state of the timeline.
See also start(), setPaused(), and stop().
[override virtual protected]
void QTimeLine::timerEvent(QTimerEvent *event)
Reimplements: QObject::timerEvent(QTimerEvent *event).
[virtual]
qreal QTimeLine::valueForTime(int msec) const
Returns the timeline value for the time msec. The 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.
See also CurveShape and frameForTime().