QChronoTimer 類

QChronoTimer 類提供重復 (和單發) 計時器。 更多...

頭: #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).