QChronoTimer Class

The QChronoTimer class provides repetitive and single-shot timers. 更多...

头: #include <QChronoTimer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Since: 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

详细描述

The QChronoTimer class provides a high-level programming interface for timers. To use it, create a QChronoTimer, either passing the interval to the constructor, or setting it after construction using setInterval (), connect its timeout () 信号到适当槽,并调用 start ()。从那时起,它将发射 timeout () signal at constant intervals. For example:

        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 has no singleShot() static methods, as the ones on QTimer already work with chrono types and nanoseconds resolution.

In multithreaded applications, you can use QChronoTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread::exec ()。Qt 使用计时器的 线程亲缘关系 确定哪个线程将发射 timeout () 信号。因此这,必须在其线程中启动和停止计时器;从另一线程启动计时器,是不可能的。

As a special case, a QChronoTimer with a timeout of 0ns will time out as soon as possible, though the ordering between zero timers and other sources of events is unspecified. Zero timers can be used to do some work while still providing a responsive user interface:

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

从那时起, processOneThing() will be called repeatedly. It should be written in such a way that it always returns quickly (for example, after processing one data item) so that Qt can deliver events to the user interface and stop the timer as soon as it has done all its work. This is the traditional way of implementing heavy work in GUI applications, but as multithreading is becoming available on more platforms, a modern alternative is doing the heavy work in a thread other than the GUI (main) thread. Qt has the QThread class, which can be used to achieve that.

精度和计时器分辨率

The accuracy of timers depends on the underlying operating system and hardware. Most platforms support requesting nano-second precision for timers (for example, libc's nanosleep ), though the accuracy of the timer will not equal this resolution in many real-world situations.

可以设置 计时器类型 to tell QChronoTimer which precision to request from the system.

For Qt::PreciseTimer , QChronoTimer will try to keep the precision at 1ns . Precise timers will never time out earlier than expected.

For Qt::CoarseTimer and Qt::VeryCoarseTimer types, QChronoTimer may wake up earlier than expected, within the margins for those types:

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

Alternatives to QChronoTimer

QChronoTimer provides nanosecond resolution and a ±292 years range (less chances of integer overflow if the interval is longer than std::numeric_limits<int>::max() ). If you only need millisecond resolution and ±24 days range, you can continue to use the classical QTimer class

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.

Some operating systems limit the number of timers that may be used; Qt does its best to work around these limitations.

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

特性文档编制

[bindable read-only] active : bool

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

此布尔特性为 true if the timer is running; otherwise false .

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

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

This property holds the timeout interval

此特性的默认值为 0ns .

A QChronoTimer with a timeout of 0ns will time out as soon as all the events in the window system's event queue have been processed.

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 std::chrono::nanoseconds

This property holds the remaining time

Returns the remaining duration until the timeout.

If the timer is inactive, the returned duration will be negative.

If the timer is overdue, the returned duration will be 0ns .

访问函数:

std::chrono::nanoseconds remainingTime () const

另请参阅 interval .

[bindable] singleShot : bool

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

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

单发计时器仅激发一次,非单发计时器被激发每隔 interval .

此特性的默认值为 false .

另请参阅 interval .

[bindable] timerType : Qt::TimerType

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

Controls the accuracy of the timer

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

另请参阅 Qt::TimerType .

成员函数文档编制

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

构造计时器采用给定 parent , using the default interval, 0ns .

[explicit] QChronoTimer:: QChronoTimer ( std::chrono::nanoseconds nsec , QObject * parent = nullptr)

构造计时器采用给定 parent , using an interval of 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 , with connection type connectionType ,并返回要连接的句柄。

This method is provided as a convenience. It's equivalent to calling:

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

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

Qt::TimerId QChronoTimer:: id () const

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

另请参阅 Qt::TimerId .

bool QChronoTimer:: isActive () const

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

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

[slot] void QChronoTimer:: start ()

此函数重载 start()。

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

若计时器已经在运行,它会被 stopped and restarted. This will also change its 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).