详细描述
...
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()));
默认情况下,时间轴线对象timeline从开始到结束运行一次,所以你想要重新从开始点运行的话你必须重新调用start()函数。要创建时间轴循环,你必须调用setLoopCount(),传递时间轴在结束前需要运行的次数。时间轴运行方向也是可以改变的,通过调用setDirection()使时间轴向后运行。你可以通过调用setPause()来暂停和取消暂停时间轴。对于交互式控件,提供setCurrentTime()函数,它可以直接设置时间轴的时间位置。虽然最常用在NotRunning(不运行)状态,(例如,连接掉QSlider的 valueChanged()信号),但是这个函数可以在任何时候被调用。
相关常用成员
enum QTimeLine::CurveShape
该枚举描述了QTimeLine值曲线的默认形状。默认值曲线形状是EaseInOutCurse。该曲线定义了值和时间轴之间的关系。
枚举常量 | 值 | 英文描述 | 中文描述 |
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). | 值呈线性增长(例如,如果时间间隔是1000ms,500ms时刻的时间值是0.5) |
QTimeLine::SineCurve | 4 | The value grows sinusoidally. | 值以正弦曲线式增长 |
QTimeLine::CosineCurve | 5 | The value grows cosinusoidally. | 值以余弦曲线式增长 |
enum QTimeLine::Direction
该枚举描述了在Running(运行)状态下时间轴的方向。
枚举常量 | 值 | 英文描述 | 中文描述 |
QTimeLine::Forward | 0 | The current time of the timeline increases with time (i.e., moves from 0 and towards the end / duration). | 时间轴的当前时间随时间的增长而增长(也就是说,从0到结束时刻方向走) |
QTimeLine::Backward | 1 | The current time of the timeline decreases with time (i.e., moves from the end / duration and towards 0). | 时间轴的当前时间随时间的增长而下降(也就是说,从结束时刻往0时刻方向走) |
enum QTimeLine::State
该枚举描述了时间轴的状态。
枚举常量 | 值 | 英文描述 | 中文描述 |
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的初始状态和完成后重新进入的状态。当前时间,帧数和保留值保持不变知道setCurrentTime()被调用或者调用了start()开启时间轴 |
QTimeLine::Paused | 1 | The timeline is paused (i.e., temporarily suspended). Calling setPaused(false) will resume timeline activity. | 时间轴被暂停了(也就是说暂时被挂起了)。调用setPaused(false)将会重新激活时间轴 |
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. | 时间轴正在运行。当控件处于时间循环,QTimeLine将会定期更新当前时间,并在适当的时机发射valueChanged()和frameChanged()信号 |
常用成员函数(API)
QTimeLine::QTimeLine(int duration = 1000, QObject *parent = Q_NULLPTR)
构造一个持续时间为duration毫秒的时间轴。参数parent是传递给QObject的构造器。默认持续时间是1000毫秒。
int QTimeLine::currentFrame() const
返回当前时间对应的帧数。
[signal] void QTimeLine::finished()
该信号在QTimeLine完成的时候发射(也就是说,到达了时间轴的末尾),而不是循环。
[signal] void QTimeLine::frameChanged(int frame)
QTimeLine在Running(运行)状态下定期发射这个信号,但只有在当前帧发生变化才会发射,参数frame是当前的帧数。
int QTimeLine::frameForTime(int msec) const
返回时间msec对应的帧数。这个值是使用开始和结束帧的线性插值算法来计算的,基于valueForTime()返回的值。
[slot] void QTimeLine::resume()
从当前时间回恢复时间轴。QTimeLine将重新进入运行状态,并且一旦进入时间循环,它将会定期地更新当前时间,帧数和值。
void QTimeLine::setEndFrame(int frame)
设置结束帧,也就是时间轴末尾对应的帧数(也就是当前值为1对应的帧数)为f参数rame帧。
void QTimeLine::setFrameRange(int startFrame, int endFrame)
设置时间轴帧数计数器开始值设置为startFrame,结尾值设置为endFrame。对于每一个时间值,QTimeLIne将会在你调用currentFrame()或者frameForValue()时通过插值算法找到对应的帧数,使用valueForTime()的返回值。
[slot] void QTimeLine::setPaused(bool paused)
如果参数paused为真,QTimeLine将会进入暂停状态。在调用start()或者setPaused(false)时没有更新信号会被反射。如果参数paused是false,时间轴将在他离开的地方继续。
void QTimeLine::setStartFrame(int frame)
设置开始帧,也就是时间轴开始对应的帧数(也就是当前值为0对应的帧数)为参数frame帧。
[slot] void QTimeLine::start()
启动时间轴。QTimeLine将进入运行状态,QTimeLine将进入运行状态,并且一旦进入事件循环,它将定期更新当前的时间,帧数和值。默认的间隔是40ms(也就是每秒更新25次)。你可以通过setUpdateInterval()改变更新的间隔。
int QTimeLine::startFrame() const
返回开始帧,即与时间轴开始时对应的帧数(也就是当前值为0的帧数)。
State QTimeLine::state() const
返回时间轴的状态(运行,不运行或者暂停)。
[signal] void QTimeLine::stateChanged(QTimeLine::State newState)
当QTimeLine的状态改变时会发射该信号。新的状态时参数newState。
[slot] void QTimeLine::stop()
停止时间轴,导致QTimeLine进入NotRunning(不运行)状态。
[slot] void QTimeLine::toggleDirection()
切换时间轴的方向。如果方向是向前的,它就会向后,反之,则相反。
[signal] void QTimeLine::valueChanged(qreal value)
QTimeLine会在运行状态下定时地发射该信号,但是只有在当前值改变了才会发射。参数值value是当前值。参数值value介于0.0到1.0之间。
[virtual] qreal QTimeLine::valueForTime(int msec) const
返回时间mesc对应的值。返回值会因插值算法对应的曲线形状curseShape的不同而不同,返回值始终介于0.0和1.0之间。如果参数mesc是0,通常默认的实现是返回0。