QChronoTimer Class
QChronoTimer 类提供了循环定时器和单次定时器。更多内容...
| 头文件: | #include <QChronoTimer> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 自: | Qt 6.8 |
| 继承自: | QObject |
- 所有成员列表(包括继承的成员)
- QChronoTimer 属于事件类。
属性
|
|
公共函数
| QChronoTimer(QObject *parent = nullptr) | |
| QChronoTimer(std::chrono::nanoseconds nsec, QObject *parent = nullptr) | |
| virtual | ~QChronoTimer() override |
| QBindable<bool> | bindableActive() |
| QBindable<std::chrono::nanoseconds> | bindableInterval() |
| QBindable<bool> | bindableSingleShot() |
| QBindable<Qt::TimerType> | bindableTimerType() |
| QMetaObject::Connection | callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection) |
| Qt::TimerId | id() const |
| std::chrono::nanoseconds | interval() const |
| bool | isActive() const |
| bool | isSingleShot() const |
| std::chrono::nanoseconds | remainingTime() const |
| void | setInterval(std::chrono::nanoseconds nsec) |
| void | setSingleShot(bool singleShot) |
| void | setTimerType(Qt::TimerType atype) |
| Qt::TimerType | timerType() const |
公共槽
信号
| void | timeout() |
重新实现的受保护函数
| virtual void | timerEvent(QTimerEvent *e) override |
详细说明
QChronoTimer 类为定时器提供了一个高级编程接口。要使用它,请创建一个 QChronoTimer 对象——既可以通过在构造函数中传入间隔来创建,也可以在创建后使用setInterval() 方法设置间隔,然后将其timeout() 信号连接到相应的槽函数,并调用start() 方法。 此后,它将以恒定的间隔发出timeout()信号。例如:
auto *timer = new QChronoTimer(1s, this);
connect(timer, &QChronoTimer::timeout, this, &MyWidget::processOneThing);
timer->start();
QChronoTimer *timer = new QChronoTimer(this);
connect(timer, &QChronoTimer::timeout, this, &MyWidget::processOneThing);
timer->setInterval(1s);
timer->start();您可以通过调用setSingleShot(true) 来设置定时器仅触发一次。
注意:QChronoTimer 没有 singleShot() 静态方法,因为QTimer 上的方法已经支持 chrono 类型和纳秒级分辨率。
在多线程应用程序中,您可以在任何具有事件循环的线程中使用 QChronoTimer。若要在非 GUI 线程中启动事件循环,请使用QThread::exec()。 Qt 会通过定时器的 `thread affinity ` 属性来确定哪个线程将发出 `timeout()` 信号。因此,您必须在该定时器所属的线程中启动和停止定时器;无法从其他线程启动定时器。
作为特例,超时设置为 `0ns ` 的 `QChronoTimer` 将尽快超时,尽管零计时器与其他事件源之间的执行顺序未作规定。零计时器可用于执行某些任务,同时仍能保持用户界面的响应性:
// The default interval is 0ns
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MyWidget::processOneThing);
timer->start();此后,processOneThing() 将被反复调用。应将其编写为始终快速返回(例如,在处理完一个数据项后),以便 Qt 能向用户界面传递事件,并在完成所有工作后立即停止计时器。 这是在 GUI 应用程序中处理繁重任务的传统方法,但随着多线程技术在更多平台上变得可用,一种现代的替代方案是在 GUI(主)线程之外的另一个线程中执行繁重任务。Qt 提供了QThread 类,可用于实现这一目的。
精度与定时器分辨率
计时器的精度取决于底层操作系统和硬件。大多数平台支持为计时器请求纳秒级精度(例如,libc 的 `nanosleep`),尽管在许多实际应用场景中,计时器的精度并不会达到这一分辨率。
您可以设置 `timer type ` 参数,以告知 QChronoTimer 应向系统请求何种精度。
对于Qt::PreciseTimer ,QChronoTimer将尝试将精度保持在1ns 。精确计时器绝不会比预期更早超时。
对于Qt::CoarseTimer 和Qt::VeryCoarseTimer 类型,QChronoTimer可能会在预期时间之前唤醒,但仍在这些类型的容差范围内:
- 间隔的 5% 对于Qt::CoarseTimer
500ms对于Qt::VeryCoarseTimer
如果系统繁忙或无法提供所请求的精度,所有计时器类型都可能比预期更晚超时。在这种超时超限的情况下,即使有多个超时已到期,Qt 也会仅触发一次 `timeout()`,然后恢复原始间隔。
QChronoTimer 的替代方案
QChronoTimer 提供纳秒级分辨率和 ±292 年的时间范围(如果时间间隔长于std::numeric_limits<int>::max() ,则整数溢出的可能性较小)。如果您仅需毫秒级分辨率和 ±24 天的时间范围,则可以继续使用经典的QTimer 类
另一种替代方案是在你的类中(该类必须是QObject 的子类)重写QObject::timerEvent()方法,并采用以下任一方法:
- 使用QBasicTimer ,这是一个封装计时器 ID 的轻量级值类。你可以通过QBasicTimer::start() 启动计时器,并通过QBasicTimer::stop() 停止计时器。你可以在重写的timerEvent() 中处理该事件。
- 一种更底层的方法是直接操作定时器 ID。要启动定时器,请调用QObject::startTimer(),并将返回的 ID 保存下来。要停止定时器,请调用QObject::killTimer()。您可以在重新实现的timerEvent() 中处理该事件。与使用QBasicTimer 相比,这种方法通常更为繁琐。
使用timerEvent() 的一个缺点是,某些高级功能(例如单次定时器和信号)不受支持。
某些操作系统会限制可使用的定时器数量;Qt 会尽力绕过这些限制。
另请参阅 QBasicTimer 、QTimerEvent 、QObject::timerEvent()、定时器和 模拟时钟。
属性文档
[bindable read-only] active : bool
注意:此 属性支持QProperty 绑定。
如果定时器正在运行,则此布尔属性为true ;否则为false 。
访问函数:
| bool | isActive() const |
[bindable] interval : std::chrono::nanoseconds
注意:此 属性支持QProperty 绑定。
该属性存储超时间隔
该属性的默认值为0ns 。
一个超时时间为0ns 的QChronoTimer 将在窗口系统事件队列中的所有事件处理完毕后立即超时。
设置正在运行的定时器的间隔将更改该定时器的间隔,stop() 随后start() 该定时器,并获取一个新的id()。如果定时器未运行,则仅更改间隔。
从 Qt 6.10 开始,设置负间隔会触发运行时警告,且该值将被重置为 1 毫秒。 在 Qt 6.10 之前,虽然可以为 Qt Timer 设置负间隔,但其行为可能出人意料(例如,如果计时器正在运行则停止,或者根本不启动)。
访问函数:
| std::chrono::nanoseconds | interval() const |
| void | setInterval(std::chrono::nanoseconds nsec) |
另请参阅 singleShot 。
[read-only] remainingTime : std::chrono::nanoseconds
该属性保存剩余时间
返回距离超时结束的剩余时长。
如果计时器处于非活动状态,则返回的时长将为负数。
如果计时器已过期,则返回的时长为0ns 。
访问函数:
| std::chrono::nanoseconds | remainingTime() const |
另请参阅 interval 。
[bindable] singleShot : bool
注意:此 属性支持QProperty 绑定。
该属性用于标识定时器是否为单次定时器
单次触发定时器仅触发一次,而非单次触发定时器则每interval 触发一次。
该属性的默认值为false 。
访问函数:
| bool | isSingleShot() const |
| void | setSingleShot(bool singleShot) |
另请参阅 interval 。
[bindable] timerType : Qt::TimerType
注意:此 属性支持QProperty 绑定。
控制计时器的精度
该属性的默认值为Qt::CoarseTimer 。
访问函数:
| Qt::TimerType | timerType() const |
| void | setTimerType(Qt::TimerType atype) |
另请参阅 Qt::TimerType 。
成员函数文档
[explicit] QChronoTimer::QChronoTimer(QObject *parent = nullptr)
根据给定的parent ,使用默认间隔0ns 创建一个定时器。
[explicit] QChronoTimer::QChronoTimer(std::chrono::nanoseconds nsec, QObject *parent = nullptr)
根据给定的parent ,并使用间隔nsec ,创建一个定时器。
[override virtual noexcept] QChronoTimer::~QChronoTimer()
销毁计时器。
template <typename Functor> QMetaObject::Connection QChronoTimer::callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
创建从timeout()信号到slot 的连接,将其放置在context 的特定事件循环中,连接类型为connectionType ,并返回该连接的句柄。
提供此方法是为了方便使用。它等同于调用:
QObject::connect(timer, &QChronoTimer::timeout, context, slot, connectionType);另请参阅 QObject::connect() 和timeout()。
Qt::TimerId QChronoTimer::id() const
如果定时器正在运行,则返回表示该定时器 ID 的 `Qt::TimerId `;否则返回 `Qt::TimerId::Invalid`。
另请参阅 Qt::TimerId 。
bool QChronoTimer::isActive() const
如果定时器正在运行,则返回true ;否则返回false 。
注意: 这是属性active 的获取器 函数。
[slot] void QChronoTimer::start()
启动或重启计时器,其超时时间由interval 中指定的值决定。
如果计时器已经在运行,则会将其stopped 并重新启动。这也会更改其id()。
如果singleShot 为true,则计时器仅会被激活一次。
[slot] void QChronoTimer::stop()
停止计时器。
另请参阅 start()。
[private signal] void QChronoTimer::timeout()
当计时器超时后,将发出此信号。
注意:这是一个 私有信号。它可以在信号连接中使用,但用户无法触发该信号。
另请参阅 interval 、start() 以及stop()。
[override virtual protected] void QChronoTimer::timerEvent(QTimerEvent *e)
重写了:QObject::timerEvent(QTimerEvent *event)。
© 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.