QTimer 类

QTimer 类提供重复 (和单发) 计时器。 更多...

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

特性

公共函数

QTimer (QObject * parent = nullptr)
virtual ~QTimer ()
QBindable<bool> bindableActive ()
QBindable<int> bindableInterval ()
QBindable<bool> bindableSingleShot ()
QBindable<Qt::TimerType> bindableTimerType ()
QMetaObject::Connection callOnTimeout (Functor && slot )
QMetaObject::Connection callOnTimeout (const QObject * context , Functor && slot , Qt::ConnectionType connectionType = Qt::AutoConnection)
(从 6.8 起) Qt::TimerId id () const
int interval () const
std::chrono::milliseconds intervalAsDuration () const
bool isActive () const
bool isSingleShot () const
int remainingTime () const
std::chrono::milliseconds remainingTimeAsDuration () const
void setInterval (int msec )
void setInterval (std::chrono::milliseconds value )
void setSingleShot (bool singleShot )
void setTimerType (Qt::TimerType atype )
void start (std::chrono::milliseconds msec )
int timerId () const
Qt::TimerType timerType () const

公共槽

void start (int msec )
void start ()
void stop ()

信号

void timeout ()

静态公共成员

void singleShot (Duration interval , Functor && functor )
void singleShot (Duration interval , Qt::TimerType timerType , Functor && functor )
void singleShot (Duration interval , const QObject * context , Functor && functor )
void singleShot (Duration interval , Qt::TimerType timerType , const QObject * context , Functor && functor )
void singleShot (std::chrono::nanoseconds nsec , const QObject * receiver , const char * member )
void singleShot (std::chrono::nanoseconds nsec , Qt::TimerType timerType , const QObject * receiver , const char * member )

重实现保护函数

virtual void timerEvent (QTimerEvent * e ) override

详细描述

QTimer 类为计时器提供高级编程接口。要使用它,创建 QTimer,连接其 timeout () 信号到适当槽,并调用 start ()。从那时起,它将发射 timeout () 信号按常量间隔。

一秒 (1000 毫秒) 计时器范例 (来自 指针式时钟 范例):

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
    timer->start(1000);
					

从那时起, update() 槽被每秒调用。

可以将计时器设为仅超时一次,通过调用 setSingleShot (true)。也可以使用静态 QTimer::singleShot () 函数调用槽在指定间隔后:

    QTimer::singleShot(200, this, &Foo::updateCaption);
					

在多线程应用程序中,可以使用 QTimer 在拥有事件循环的任何线程中。要从非 GUI 线程启动事件循环,使用 QThread::exec ()。Qt 使用计时器的 线程亲缘关系 确定哪个线程将发射 timeout () 信号。因此这,必须在其线程中启动和停止计时器;从另一线程启动计时器,是不可能的。

作为特殊情况,采用 0 超时的 QTimer 将尽快超时,虽然未指定零计时器和其它事件源之间的次序。可以使用零计时器做某些工作,却仍提供敏捷用户界面:

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Foo::processOneThing);
    timer->start();
					

从那时起, processOneThing() 会被重复调用。应始终以快速返回的这种方式编写 (通常在处理某一数据项后),以便 Qt 可以向用户界面交付事件,并在完成所有工作后尽快停止计时器。这是在 GUI 应用程序中实现繁重工作的传统方式,但随着多线程现今在越来越多平台中变为可用,期望零毫秒 QTimer 对象将被逐渐替换为 QThread

精度和计时器分辨率

计时器的准确性取决于底层操作系统和硬件。大多数平台支持 1 毫秒的分辨率,虽然计时器的准确性在许多真实世界状况下不会等于此分辨率。

精度还取决于 计时器类型 。对于 Qt::PreciseTimer ,QTimer 将试着把精度保持在 1 毫秒。精密计时器也从不会比期望超时提前。

For Qt::CoarseTimer and Qt::VeryCoarseTimer 类型,QTimer 的唤醒可能早于期望,在这些类型的边距内:间隔 5% 对于 Qt::CoarseTimer 和 500 ms 对于 Qt::VeryCoarseTimer .

所有计时器类型的超时均可能晚于期望,若系统繁忙 (或无法提供要求的精度)。在这种超时超限情况下,Qt 会发射 timeout () 仅一次,即使有多个超时过期,然后再继续原始间隔。

替代 QTimer

Qt 6.8 introduced QChronoTimer . The main difference between the two classes, is that QChronoTimer supports a larger interval range and a higher precision ( std::chrono::nanoseconds ). For QTimer the maximum supported interval is ±24 days, whereas for QChronoTimer it is ±292 years (less chances of interger overflow with intervals longer than std::numeric_limits<int>::max() ). If you only need millisecond resolution and ±24 days range, you can continue to use QTimer.

Another alternative is reimplementing the QObject::timerEvent () method in your class (which must be a sub-class of QObject ), and using one of the following approaches:

A disadvantage of using timerEvent () is that some high-level features, such as single-shot timers and signals, aren't supported.

某些操作系统限制可能使用的计时器数;Qt 试着绕过这些局限性。

另请参阅 QBasicTimer , QTimerEvent , QObject::timerEvent (), 计时器 ,和 指针式时钟 .

特性文档编制

[bindable read-only] active : bool

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

此布尔特性为 true 若计时器正在运行;否则 false。

[bindable] interval : int

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

此特性保持超时间隔 (以毫秒为单位)

此特性的默认值为 0。 QTimer 采用 0 超时间隔会尽快超时,在已处理窗口系统事件队列中的所有事件后。

Setting the interval of a running timer will change the interval, stop () and then start () the timer, and acquire a new id (). If the timer is not running, only the interval is changed.

另请参阅 singleShot .

[read-only] remainingTime : const int

此特性保持剩余时间 (以毫秒为单位)

返回计时器的剩余值 (以毫秒为单位) 直到超时。若计时器处于非活动状态,返回值会为 -1。若计时器过期,返回值会为 0。

访问函数:

int remainingTime () const

另请参阅 interval .

[bindable] singleShot : bool

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

此特性保持计时器是否为单次计时器

单发计时器仅激发一次,非单发计时器被激发每隔 interval 毫秒。

此特性的默认值为 false .

另请参阅 interval and singleShot ().

[bindable] timerType : Qt::TimerType

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

控制计时器的精度

此特性的默认值为 Qt::CoarseTimer .

另请参阅 Qt::TimerType .

成员函数文档编制

[static] template <typename Duration, typename Functor> void QTimer:: singleShot ( Duration interval , Functor && functor )

[static] template <typename Duration, typename Functor> void QTimer:: singleShot ( Duration interval , Qt::TimerType timerType , Functor && functor )

[static] template <typename Duration, typename Functor> void QTimer:: singleShot ( Duration interval , Qt::TimerType timerType , const QObject * context , Functor && functor )

[static] template <typename Duration, typename Functor> void QTimer:: singleShot ( Duration interval , const QObject * context , Functor && functor )

此静态函数调用 functor after interval .

使用此函数非常方便,因为不需要麻烦采用 timerEvent 或创建本地 QTimer 对象。

context 有指定,那么 functor 才会被调用若 context 对象在间隔发生前未被销毁。然后函子将运行于线程对于 context 。上下文线程必须拥有正在运行的 Qt 事件循环。

functor 是成员函数对于 context ,那么将在对象中调用函数。

The interval 参数可以是 int (interpreted as a millisecond count) or a std::chrono type that implicitly converts to nanoseconds.

注意: In Qt versions prior to 6.8, the chrono overloads took chrono::milliseconds, not chrono::nanoseconds. The compiler will automatically convert for you, but the conversion may overflow for extremely large milliseconds counts.

注意: 此函数是 可重入 .

另请参阅 start ().

[explicit] QTimer:: QTimer ( QObject * parent = nullptr)

构造计时器采用给定 parent .

[virtual noexcept] QTimer:: ~QTimer ()

销毁计时器。

template <typename Functor> QMetaObject::Connection QTimer:: callOnTimeout ( Functor && slot )

创建连接从计时器的 timeout () 信号到 slot 。返回要连接的句柄。

提供此方法是为了方便。相当于调用:

QObject::connect(timer, &QTimer::timeout, timer, slot, Qt::DirectConnection);
					

注意: 此重载不可用当 QT_NO_CONTEXTLESS_CONNECT 有定义,使用接受上下文对象的 callOnTimeout() 重载代替。

另请参阅 QObject::connect () 和 timeout ().

template <typename Functor> QMetaObject::Connection QTimer:: callOnTimeout (const QObject * context , Functor && slot , Qt::ConnectionType connectionType = Qt::AutoConnection)

此函数重载 callOnTimeout()。

创建连接从 timeout () 信号到 slot 以放置在特定事件循环 context ,并返回要连接的句柄。

提供此方法是为了方便。相当于调用:

QObject::connect(timer, &QTimer::timeout, context, slot, connectionType);
					

另请参阅 QObject::connect () 和 timeout ().

[since 6.8] Qt::TimerId QTimer:: id () const

返回 Qt::TimerId representing the timer ID if the timer is running; otherwise returns Qt::TimerId::Invalid .

该函数在 Qt 6.8 引入。

另请参阅 Qt::TimerId .

std::chrono::milliseconds QTimer:: intervalAsDuration () const

把此计时器的间隔返回作为 std::chrono::milliseconds 对象。

另请参阅 interval .

bool QTimer:: isActive () const

返回 true if the timer is running; otherwise returns false .

注意: getter 函数对于特性 active .

std::chrono::milliseconds QTimer:: remainingTimeAsDuration () const

把此计时器对象的剩余时间返回作为 std::chrono::milliseconds 对象。若此计时器到期 (或过期),返回值为 std::chrono::milliseconds::zero() . If the remaining time could not be found or the timer is not running, this function returns a negative duration.

另请参阅 remainingTime ().

[static] void QTimer:: singleShot ( std::chrono::nanoseconds nsec , const QObject * receiver , const char * member )

这是重载函数。

此静态函数调用槽,在给定时间间隔后。

使用此函数非常方便,因为不需要麻烦采用 timerEvent 或创建本地 QTimer 对象。

The receiver 是接收对象而 member 是槽。时间间隔被给出由持续时间对象 nsec .

注意: In Qt versions prior to 6.8, this function took chrono::milliseconds, not chrono::nanoseconds. The compiler will automatically convert for you, but the conversion may overflow for extremely large milliseconds counts.

注意: 此函数是 可重入 .

另请参阅 start ().

[static] void QTimer:: singleShot ( std::chrono::nanoseconds nsec , Qt::TimerType timerType , const QObject * receiver , const char * member )

这是重载函数。

此静态函数调用槽,在给定时间间隔后。

使用此函数非常方便,因为不需要麻烦采用 timerEvent 或创建本地 QTimer 对象。

The receiver 是接收对象而 member 是槽。时间间隔被给出由持续时间对象 nsectimerType 影响计时器精度。

注意: In Qt versions prior to 6.8, this function took chrono::milliseconds, not chrono::nanoseconds. The compiler will automatically convert for you, but the conversion may overflow for extremely large milliseconds counts.

注意: 此函数是 可重入 .

另请参阅 start ().

[slot] void QTimer:: start ( int msec )

启动 (或重启) 计时器采用超时间隔 msec 毫秒。

这相当于:

timer.setInterval(msec);
timer.start();
					

若计时器已经在运行,它会被 stopped and restarted. This will also change its id ().

singleShot 为 true,将仅激活计时器一次。

注意: 采用 0 计时器使事件循环保持忙碌必然导致故障, 且 UI 的行为会高度不稳定。

[slot] void QTimer:: start ()

此函数重载 start()。

启动 (或重启) 计时器采用指定超时在 interval .

若计时器已经在运行,它会被 stopped and restarted. This will also change its id ().

singleShot 为 true,将仅激活计时器一次。

void QTimer:: start ( std::chrono::milliseconds msec )

这是重载函数。

启动 (或重启) 计时器,采用超时持续时间 msec 毫秒。

这相当于:

timer.setInterval(msec);
timer.start();
					

若计时器已经在运行,它会被 stopped and restarted. This will also change its id ().

singleShot 为 true,将仅激活计时器一次。

[slot] void QTimer:: stop ()

停止计时器。

另请参阅 start ().

[private signal] void QTimer:: timeout ()

此信号被发射当计时器超时。

注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。

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

[override virtual protected] void QTimer:: timerEvent ( QTimerEvent * e )

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

int QTimer:: timerId () const

返回计时器的 ID,若计时器正在运行;否则返回 -1。