本页内容

QTimeLine Class

QTimeLine 类提供了一个用于控制动画的时间轴。更多内容...

头文件: #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

详细说明

它最常用的方式是定期调用一个槽,从而对 GUI 控件进行动画处理。 您可以通过将持续时间(以毫秒为单位)传递给 QTimeLine 的构造函数来构建一条时间轴。时间轴的持续时间描述了动画将运行多长时间。然后,通过调用setFrameRange() 来设置合适的帧范围。最后,将frameChanged() 信号连接到您希望进行动画处理的小部件中的合适槽(例如,QProgressBar 中的setValue())。 当你调用start() 时,QTimeLine 将进入 Running 状态,并开始以固定间隔发出frameChanged() 信号,从而使小部件中连接的属性的值以稳定的速率从帧范围的下限逐渐增长到上限。 您可以通过调用setUpdateInterval()来指定更新间隔。完成后,QTimeLine将进入NotRunning 状态,并发出finished()事件。

示例:

//...
auto 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
auto 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 属性将被忽略。

另请参阅 QProgressBar 和QProgressDialog 。

成员类型文档

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。

访问函数:

int currentTime() const
void setCurrentTime(int msec)

[bindable] direction : Direction

注意:此 属性支持QProperty 绑定。

当QTimeLine 处于Running 状态时,该属性保存时间轴的方向。

该方向指示时间是從 0 向时间轴时长方向移动,还是在调用start() 之后,从时长值向 0 方向移动。

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

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

访问函数:

QTimeLine::Direction direction() const
void setDirection(QTimeLine::Direction direction)

[bindable] duration : int

注意:此 属性支持QProperty 绑定。

该属性存储时间轴的总时长(以毫秒为单位)。

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

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

访问函数:

int duration() const
void setDuration(int duration)

[bindable] easingCurve : QEasingCurve

注意:此 属性支持QProperty 绑定。

指定时间轴将使用的缓动曲线。如果重写了valueForTime() 方法,则此值将被忽略。

访问函数:

QEasingCurve easingCurve() const
void setEasingCurve(const QEasingCurve &curve)

另请参阅 valueForTime()。

[bindable] loopCount : int

注意:此 属性支持QProperty 绑定。

该属性用于指定时间轴在结束前应循环的次数。

循环计数为 0 表示时间轴将无限循环。

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

访问函数:

int loopCount() const
void setLoopCount(int count)

[bindable] updateInterval : int

注意:此 属性支持QProperty 绑定。

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

在更新当前时间时,如果当前值发生了变化,QTimeLine 将触发valueChanged()事件;如果帧发生了变化,则触发frameChanged()事件。

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

访问函数:

int updateInterval() const
void setUpdateInterval(int interval)

成员函数文档

[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 将重新进入“Running”状态,一旦进入事件循环,它将以固定间隔更新其当前时间、帧和值。

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

另请参阅 start()、updateInterval()、frameChanged() 以及valueChanged()。

void QTimeLine::setEndFrame(int frame)

将结束帧(即对应于时间轴末尾的帧,也就是当前值为 1 的帧)设置为frame 。

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

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

将时间轴的帧计数器设置为从startFrame 开始,到endFrame 结束。对于每个时间值,当您调用currentFrame()或frameForTime()时,QTimeLine 会通过使用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 表示当前值。value 是一个介于0.0和1.0之间的数值

注意:这是一个 私有信号。它可以在信号连接中使用,但用户无法触发该信号。

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

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

返回时间点msec 对应的时间轴值。返回值会根据曲线形状而有所不同,但始终介于0和1之间。如果msec 为0,则默认实现始终返回0。

重写此函数以向时间轴提供自定义曲线形状。

另请参阅 easingCurve 和frameForTime()。

© 2026 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.