QChronoTimer Class

QChronoTimer 类提供重复定时器和单次定时器。更多

头文件: #include <QChronoTimer>
CMake.QChronoTimer find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Qt 6.8
继承: QObject

属性

公共功能

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 start()
void stop()

信号

void timeout()

重新实现的受保护函数

virtual void timerEvent(QTimerEvent *e) override

详细说明

QChronoTimer 类为定时器提供了高级编程接口。要使用它,只需创建一个 QChronoTimer,将时间间隔传递给构造函数,或在构造函数完成后使用setInterval() 设置时间间隔,将timeout() 信号连接到相应的插槽,然后调用start() 即可。从那时起,它将以恒定的时间间隔发出timeout() 信号。例如

        QChronoTimer *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 上的静态方法已经可以使用计时器类型和纳秒分辨率。

在多线程应用程序中,您可以在任何有事件循环的线程中使用 QChronoTimer。要从非 GUI 线程启动事件循环,请使用QThread::exec() 。Qt 使用定时器的thread affinity 来确定哪个线程将发出timeout() 信号。因此,您必须在其线程中启动和停止定时器;不可能从其他线程启动定时器。

作为一种特例,超时时间为0ns 的 QChronoTimer 将尽快超时,但零定时器和其他事件源之间的排序未作规定。零定时器可以用来完成一些工作,同时还能提供反应灵敏的用户界面:

        // The default interval is 0ns
        QChronoTimer *timer = new QChronoTimer(this);
        connect(timer, &QChronoTimer::timeout, this, &MyWidget::processOneThing);
        timer->start();

从那时起,processOneThing() 将被反复调用。它的编写方式应使其始终快速返回(例如,处理完一个数据项后),这样 Qt Quick 就能向用户界面传递事件,并在完成所有工作后立即停止定时器。这是 GUI 应用程序中实现繁重工作的传统方法,但随着多线程技术在更多平台上的应用,一种现代的替代方法是在 GUI(主)线程以外的线程中完成繁重的工作。Qt XML 拥有QThread 类,可用于实现这一点。

精度和定时器分辨率

定时器的精度取决于底层操作系统和硬件。大多数平台都支持为定时器请求纳秒级精度(例如,libc 的nanosleep ),不过在许多实际情况下,定时器的精度并不等同于这一分辨率。

您可以设置timer type ,告诉 QChronoTimer 向系统请求的精度。

对于Qt::PreciseTimer ,QChronoTimer 会尽量将精度保持在1ns 。精确定时器绝不会提前超时。

对于Qt::CoarseTimerQt::VeryCoarseTimer 类型,QChronoTimer 可能会比预期提前唤醒,但不会超出这些类型的范围:

如果系统繁忙或无法提供所需的精度,所有定时器类型的超时时间都可能晚于预期。在这种超时超限的情况下,Qt 将只发出一次timeout() ,即使多次超时已过,然后将恢复原来的时间间隔。

QChronoTimer 的替代方案

QChronoTimer 提供纳秒级的分辨率和 ±292 年的范围(如果时间间隔长于std::numeric_limits<int>::max() ,则整数溢出的可能性较小)。如果只需要毫秒分辨率和 ±24 天范围,则可以继续使用经典的QTimer 类。

另一种方法是在您的类(必须是QObject 的子类)中重新实现QObject::timerEvent() 方法,并使用以下方法之一:

使用timerEvent() 的缺点是不支持某些高级功能,如单发定时器和信号。

有些操作系统限制了可使用的定时器数量;Qt 会尽力绕过这些限制。

另请参阅 QBasicTimer,QTimerEvent,QObject::timerEvent(),定时器模拟时钟

属性文档

[bindable read-only] active : bool

注: 此属性支持QProperty 绑定。

如果定时器正在运行,则该布尔属性为true ;否则为false

[bindable] interval : std::chrono::nanoseconds

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

该属性保存超时时间间隔

该属性的默认值为0ns

超时时间为0nsQChronoTimer 会在窗口系统事件队列中的所有事件都处理完毕后立即超时。

设置正在运行的定时器的时间间隔将改变时间间隔,stop() 然后start() 定时器,并获取新的id() 。如果定时器没有运行,则只更改时间间隔。

另请参阅 singleShot

[read-only] remainingTime : const std::chrono::nanoseconds

此属性保存剩余时间

返回超时前的剩余时间。

如果计时器处于非活动状态,返回的持续时间将为负值。

如果计时器超时,返回的持续时间将是0ns

访问函数:

std::chrono::nanoseconds remainingTime() const

另请参阅 interval

[bindable] singleShot : bool

注: 该属性支持QProperty 绑定。

该属性表示定时器是否为单发定时器

单发定时器只触发一次,非单发定时器每interval 触发一次。

此属性的默认值为false

另请参阅 interval

[bindable] timerType : Qt::TimerType

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

控制计时器的精度

该属性的默认值为Qt::CoarseTimer

另请参阅 Qt::TimerType

成员函数文档

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

使用默认时间间隔0ns ,以给定的parent 构建一个定时器。

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

该函数重载 callOnTimeout()。

创建一个从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()

此函数重载 start()。

interval 中指定的超时时间启动或重启定时器。

如果定时器已在运行,则将stopped 并重新启动。这也将改变id() 。

如果singleShot 为 true,定时器将只启动一次。

[slot] void QChronoTimer::stop()

停止计时器。

另请参阅 start().

[private signal] void QChronoTimer::timeout()

计时器超时时发出该信号。

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

另请参阅 intervalstart() 和stop()。

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

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

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