QTimeLine Class

QTimeLine 类提供了用于控制动画的时间线。更多

Header: #include <QTimeLine>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承: QObject

公共类型

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

属性

公共功能

QTimeLine(int duration = 1000, QObject *parent = nullptr)
virtual ~QTimeLine()
QBindable<int> bindableCurrentTime()
QBindable<QTimeLine::Direction> bindableDirection()
QBindable<int> bindableDuration()
QBindable<QEasingCurve> bindableEasingCurve()
QBindable<int> bindableLoopCount()
QBindable<int> bindableUpdateInterval()
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

公共插槽

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

详细说明

QTimeLine 是一种时间线(timeline)函数,最常用于通过周期性调用槽来制作 GUI 控件的动画。您可以通过向 QTimeLine 的构造函数传递以毫秒为单位的持续时间来构造时间线。时间线的持续时间描述了动画将运行多长时间。然后调用setFrameRange() 设置合适的帧范围。最后,将frameChanged() 信号连接到要制作动画的 widget 中的合适插槽(例如,QProgressBar 中的setValue() )。当您继续调用start() 时,QTimeLine 将进入运行状态,并开始以固定的时间间隔发出frameChanged() 信号,使部件的连接属性值以稳定的速度从帧范围的下端增长到上端。您可以通过调用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() 并发出valueChanged() 来生成介于 0 和 1 之间的值。默认情况下,valueForTime() 采用插值算法生成这些值。您可以调用setEasingCurve() 从一组预定义的时间线算法中进行选择。

请注意,默认情况下,QTimeLine 使用QEasingCurve::InOutSine ,该算法提供的值会缓慢增长,然后稳定增长,最后缓慢增长。对于自定义时间线,您可以重新实现valueForTime() ,在这种情况下,QTimeLine 的easingCurve 属性将被忽略。

另请参阅 QProgressBarQProgressDialog

成员类型文档

enum QTimeLine::Direction

该枚举描述了时间线在Running 状态下的方向。

常量描述
QTimeLine::Forward0时间线的当前时间随时间增加(即从 0 开始向终点/持续时间方向移动)。
QTimeLine::Backward1时间轴的当前时间随时间减少(即从结束/持续时间向 0 移动)。

另请参阅 setDirection().

enum QTimeLine::State

该枚举描述时间线的状态。

常量说明
QTimeLine::NotRunning0时间轴未运行。这是QTimeLine 的初始状态,也是QTimeLine 结束后重新进入的状态。当前时间、帧和值保持不变,直到调用setCurrentTime() 或通过调用start() 启动时间线。
QTimeLine::Paused1时间线暂停(即暂时中止)。调用setPaused(false) 将恢复时间线活动。
QTimeLine::Running2时间线正在运行。当控制处于事件循环中时,QTimeLine 将定期更新当前时间,并在适当的时候发出valueChanged() 和frameChanged() 。

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

属性文档

[bindable] currentTime : int

注: 该属性支持QProperty 绑定。

该属性保存时间线的当前时间。

QTimeLine 处于运行状态时,该值会根据时间线的持续时间和方向不断更新。否则,该值就是上次调用stop() 时的当前值,或者是 setCurrentTime() 设置的值。

注: 您可以将其他属性绑定到 currentTime,但不建议对其进行绑定。随着动画的进行,currentTime 会自动更新,从而取消绑定。

默认情况下,此属性的值为 0。

[bindable] direction : Direction

注: 该属性支持QProperty 绑定。

QTimeLine 处于Running 状态时,该属性保留时间线的方向。

该方向表示时间是从 0 开始向时间线持续时间移动,还是在调用start() 后从持续时间值开始向 0 移动。

任何方向绑定不仅会被 setDirection() 删除,也会被toggleDirection() 删除。

默认情况下,此属性设置为Forward

[bindable] duration : int

注: 该属性支持QProperty 绑定。

该属性以毫秒为单位保存时间线的总持续时间。

默认情况下,该值为 1000(即 1 秒),但您可以通过向QTimeLine 的构造函数传递持续时间或调用 setDuration() 来更改该值。持续时间必须大于 0。

注意: 更改持续时间不会导致当前时间重置为零或新的持续时间。您还需要调用setCurrentTime() 并输入所需的值。

[bindable] easingCurve : QEasingCurve

注: 该属性支持QProperty 绑定。

指定时间线将使用的缓和曲线。如果valueForTime() 被重新实现,此值将被忽略。

另请参阅 valueForTime()。

[bindable] loopCount : int

注: 该属性支持QProperty 绑定。

该属性用于保存时间线结束前的循环次数。

循环次数为 0 意味着时间线将永远循环。

默认情况下,该属性的值为 1。

[bindable] updateInterval : int

注: 该属性支持QProperty 绑定。

该属性用于保存QTimeLine 每次更新当前时间的间隔时间(以毫秒为单位)。

更新当前时间时,如果当前值发生变化,QTimeLine 将发出valueChanged() ;如果帧发生变化,frameChanged() 。

默认情况下,时间间隔为 40 毫秒,相当于每秒更新 25 次。

成员函数文档

[explicit] QTimeLine::QTimeLine(int duration = 1000, QObject *parent = nullptr)

构建一个持续时间为duration 毫秒的时间线。parent 将传递给QObject 的构造函数。默认持续时间为 1000 毫秒。

[virtual noexcept] QTimeLine::~QTimeLine()

破坏时间线

int QTimeLine::currentFrame() const

返回与当前时间相对应的帧。

另请参阅 currentTime()、frameForTime() 和setFrameRange()。

qreal QTimeLine::currentValue() const

返回与当前时间相对应的值。

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

int QTimeLine::endFrame() const

返回结束帧,即时间线结束时对应的帧(即当前值为 1 的帧)。

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

[private signal] void QTimeLine::finished()

QTimeLine 结束(即到达时间线的终点)时发出该信号,不会循环。

注意: 这是一个私有信号。可以在信号连接中使用,但用户不能发出。

[private signal] void QTimeLine::frameChanged(int frame)

QTimeLine Running frame 是当前帧的编号。

注意: 这是一个专用信号。可以在信号连接中使用,但用户不能发出。

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

int QTimeLine::frameForTime(int msec) const

返回与时间msec 相对应的帧。该值是根据valueForTime() 返回的值,使用开始帧和结束帧的线性插值计算得出的。

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

[slot] void QTimeLine::resume()

QTimeLine 将重新进入运行状态,一旦进入事件循环,就会定期更新当前时间、帧和值。

start() 不同的是,该函数在恢复之前不会重新启动时间线。

另请参见 start()、updateInterval()、frameChanged() 和valueChanged()。

void QTimeLine::setEndFrame(int frame)

将结束帧,即时间线结束时对应的帧(即当前值为 1 的帧),设置为frame

另请参阅 endFrame()、startFrame() 和setFrameRange()。

void QTimeLine::setFrameRange(int startFrame, int endFrame)

设置时间线的帧计数器,从startFrame 开始,到endFrame 结束。对于每个时间值,QTimeLine 都会在调用currentFrame() 或frameForTime() 时,利用valueForTime() 的返回值,通过插值找到相应的帧。

处于运行状态时,当帧发生变化时,QTimeLine 也会发出frameChanged() 信号。

另请参见 startFrame()、endFrame()、start() 和currentFrame()。

[slot] void QTimeLine::setPaused(bool paused)

如果paused 为 true,时间线将暂停,导致QTimeLine 进入暂停状态。在调用start() 或 setPaused(false) 之前,不会发出更新信号。如果paused 为 false,时间线将恢复并继续之前的状态。

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

void QTimeLine::setStartFrame(int frame)

将起始帧,即时间线起始帧(即当前值为 0 的帧)设置为frame

另请参阅 startFrame()、endFrame() 和setFrameRange()。

[slot] void QTimeLine::start()

启动时间线。QTimeLine 将进入运行状态,一旦进入事件循环,就会定期更新当前时间、帧和值。默认间隔为 40 毫秒(即每秒 25 次)。您可以通过调用setUpdateInterval() 来更改更新间隔。

时间线将从位置 0 开始,如果向后,则从终点开始。如果想在不重新启动的情况下恢复停止的时间线,可以调用resume() 代替。

另请参阅 resume()、updateInterval()、frameChanged() 和valueChanged()。

int QTimeLine::startFrame() const

返回起始帧,即时间线起始帧(即当前值为 0 的帧)。

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

QTimeLine::State QTimeLine::state() const

返回时间线的状态。

另请参阅 start()、setPaused() 和stop()。

[private signal] void QTimeLine::stateChanged(QTimeLine::State newState)

每当QTimeLine 的状态发生变化时,就会发出该信号。新的状态是newState

注意: 这是一个私有信号。可以在信号连接中使用,但用户不能发出。

[slot] void QTimeLine::stop()

停止时间线,使QTimeLine 进入NotRunning 状态。

另请参阅 start().

[override virtual protected] void QTimeLine::timerEvent(QTimerEvent *event)

重实现:QObject::timerEvent(QTimerEvent *event).

[slot] void QTimeLine::toggleDirection()

切换时间线的方向。如果方向是 "向前",则会变成 "向后",反之亦然。

direction 的现有绑定将被移除。

另请参见 setDirection().

[private signal] void QTimeLine::valueChanged(qreal value)

QTimeLine Running value 是当前值。 是介于 0.0 和 1.0 之间的数字。value

注意: 这是一个专用信号。可以在信号连接中使用,但用户不能发出。

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

[virtual] qreal QTimeLine::valueForTime(int msec) const

返回时间msec 的时间线值。返回值因曲线形状而异,总是介于 0 和 1 之间。如果msec 为 0,默认实现总是返回 0。

重新实现此函数可为您的时间线提供自定义的曲线形状。

另请参阅 easingCurveframeForTime()。

© 2025 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 the GNU 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.