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 interval )
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 支持更大間隔範圍和更高精度 ( 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。

訪問函數:

bool isActive () const

[bindable] interval : int

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

此特性保持超時間隔 (以毫秒為單位)

此特性的默認值為 0。 QTimer 采用 0 超時間隔會盡快超時,在已處理窗口係統事件隊列中的所有事件後。

注意: 采用 0 計時器使事件循環保持忙碌必然導緻故障, 且 UI 的行為會高度不穩定。

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.

從 Qt 6.10 開始,設置負間隔會導緻運行時警告,且值被重置為 1ms。在 Qt 6.10 之前,Qt 計時器允許設置負間隔,但行為方式齣人意料 (例如:停止計時器,若在運行或根本未啓動)。

訪問函數:

int interval () const
void setInterval (int msec )
void setInterval (std::chrono::milliseconds value )

另請參閱 singleShot .

[read-only] remainingTime : const int

此特性保持剩餘時間 (以毫秒為單位)

返迴計時器的剩餘值 (以毫秒為單位) 直到超時。若計時器處於非活動狀態,返迴值會為 -1。若計時器過期,返迴值會為 0。

訪問函數:

int remainingTime () const

另請參閱 interval .

[bindable] singleShot : bool

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

此特性保持計時器是否為單次計時器

單發計時器僅激發一次,非單發計時器被激發每隔 interval 毫秒。

此特性的默認值為 false .

訪問函數:

bool isSingleShot () const
void setSingleShot (bool singleShot )

另請參閱 interval and singleShot ().

[bindable] timerType : Qt::TimerType

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

控製計時器的精度

此特性的默認值為 Qt::CoarseTimer .

訪問函數:

Qt::TimerType timerType () const
void setTimerType (Qt::TimerType atype )

另請參閱 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.

從 Qt 6.10 開始,設置負間隔會導緻運行時警告,且值被重置為 1ms。在 Qt 6.10 之前,Qt 計時器允許設置負間隔,但行為方式齣人意料 (例如:停止計時器,若在運行或根本未啓動)。

注意: 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)

創建連接從 timeout () 信號到 slot 以放置在特定事件循環 context ,並返迴要連接的句柄。

提供此方法是為瞭方便。相當於調用:

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

此函數重載 QTimer::callOnTimeout ().

另請參閱 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 .

從 Qt 6.10 開始,設置負間隔會導緻運行時警告,且值被重置為 1ms。在 Qt 6.10 之前,Qt 計時器允許設置負間隔,但行為方式齣人意料 (例如:停止計時器,若在運行或根本未啓動)。

注意: 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 影響計時器精度。

從 Qt 6.10 開始,設置負間隔會導緻運行時警告,且值被重置為 1ms。在 Qt 6.10 之前,Qt 計時器允許設置負間隔,但行為方式齣人意料 (例如:停止計時器,若在運行或根本未啓動)。

注意: 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,將僅激活計時器一次。

從 Qt 6.10 開始,設置負間隔會導緻運行時警告,且值被重置為 1ms。在 Qt 6.10 之前,Qt 計時器允許設置負間隔,但行為方式齣人意料 (例如:停止計時器,若在運行或根本未啓動)。

注意: 采用 0 計時器使事件循環保持忙碌必然導緻故障, 且 UI 的行為會高度不穩定。

注意: 此槽被重載。要連接到此槽:

// Connect using qOverload:
connect(timer, qOverload(&QTimer::start),
        receiver, &ReceiverClass::slot);
// Or using a lambda:
connect(timer, qOverload(&QTimer::start),
        this, [](int msec) { /* handle start */ });
					

更多範例和方式,見 連接到重載槽 .

[slot] void QTimer:: start ()

啓動 (或重啓) 計時器采用指定超時在 interval .

若計時器已經在運行,它會被 stopped and restarted. This will also change its id ().

singleShot 為 true,將僅激活計時器一次。

注意: 采用 0 計時器使事件循環保持忙碌必然導緻故障, 且 UI 的行為會高度不穩定。

注意: 此槽被重載。要連接到此槽:

// Connect using qOverload:
connect(timer, qOverload<>(&QTimer::start),
        receiver, &ReceiverClass::slot);
// Or using a lambda:
connect(timer, qOverload<>(&QTimer::start),
        this, []() { /* handle start */ });
					

更多範例和方式,見 連接到重載槽 .

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

啓動 (或重啓) 計時器,采用超時持續時間 interval 毫秒。

這相當於:

timer.setInterval(interval);
timer.start();
					

若計時器已經在運行,它會被 stopped and restarted. This will also change its id ().

singleShot 為 true,將僅激活計時器一次。

從 Qt 6.10 開始,設置負間隔會導緻運行時警告,且值被重置為 1ms。在 Qt 6.10 之前,Qt 計時器允許設置負間隔,但行為方式齣人意料 (例如:停止計時器,若在運行或根本未啓動)。

注意: 采用 0 計時器使事件循環保持忙碌必然導緻故障, 且 UI 的行為會高度不穩定。

這是重載函數。

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