QTimer 類

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

頭: #include <QTimer>
qmake: QT += core
繼承: QObject

特性

公共函數

QTimer (QObject * parent = nullptr)
virtual ~QTimer ()
QMetaObject::Connection callOnTimeout (Functor slot , Qt::ConnectionType connectionType = Qt::AutoConnection)
QMetaObject::Connection callOnTimeout (const QObject * context , Functor slot , Qt::ConnectionType connectionType = Qt::AutoConnection)
QMetaObject::Connection callOnTimeout (const QObject * receiver , MemberFunction * slot , Qt::ConnectionType connectionType = Qt::AutoConnection)
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 msec )
int timerId () const
Qt::TimerType timerType () const

公共槽

void start ()
void start (int msec )
void stop ()

信號

void timeout ()

靜態公共成員

void singleShot (int msec , const QObject * receiver , const char * member )
void singleShot (int msec , Qt::TimerType timerType , const QObject * receiver , const char * member )
void singleShot (int msec , const QObject * receiver , PointerToMemberFunction method )
void singleShot (int msec , Qt::TimerType timerType , const QObject * receiver , PointerToMemberFunction method )
void singleShot (int msec , Functor functor )
void singleShot (int msec , Qt::TimerType timerType , Functor functor )
void singleShot (int msec , const QObject * context , Functor functor )
void singleShot (int msec , Qt::TimerType timerType , const QObject * context , Functor functor )
void singleShot (std::chrono::milliseconds msec , const QObject * receiver , const char * member )
void singleShot (std::chrono::milliseconds msec , 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

使用 QTimer 的替代是調用 QObject::startTimer () 為對象並重實現 QObject::timerEvent () 事件處理程序在類中 (必須繼承 QObject )。缺點是 timerEvent () 不支持如單次計時器 (或信號) 的高級特徵。

另一替代是 QBasicTimer 。通常不那麼麻煩相比使用 QObject::startTimer () 直接。見 計時器 瞭解所有 3 種途徑的概述。

某些操作係統限製可能使用的計時器數;Qt 試著繞過這些局限性。

另請參閱 QBasicTimer , QTimerEvent , QObject::timerEvent (), 計時器 , 指針式時鍾範例 ,和 擺動範例 .

特性文檔編製

active : const bool

此布爾特性為 true 若計時器正在運行;否則 false。

該特性在 Qt 4.3 引入。

訪問函數:

bool isActive () const

interval : int

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

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

設置活動計時器的間隔會改變其 timerId ().

訪問函數:

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

另請參閱 singleShot .

remainingTime : const int

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

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

該特性在 Qt 5.0 引入。

訪問函數:

int remainingTime () const

另請參閱 interval .

singleShot : bool

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

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

此特性的默認值為 false .

訪問函數:

bool isSingleShot () const
void setSingleShot (bool singleShot )

另請參閱 interval and singleShot ().

timerType : Qt::TimerType

控製計時器的精度

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

訪問函數:

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

另請參閱 Qt::TimerType .

成員函數文檔編製

QTimer:: QTimer ( QObject * parent = nullptr)

構造計時器采用給定 parent .

[slot] void QTimer:: start ()

此函數重載 start()。

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

若計時器已經在運行,它會被 stopped 並重啓。

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

[slot] void QTimer:: start ( int msec )

啓動 (或重啓) 計時器采用超時間隔 msec 毫秒。

若計時器已經在運行,它會被 stopped 並重啓。

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

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

[slot] void QTimer:: stop ()

停止計時器。

另請參閱 start ().

[signal] void QTimer:: timeout ()

此信號被發射當計時器超時。

注意: 這是私有信號。它可以用於信號連接,但不能由用戶發射。

另請參閱 interval , start (),和 stop ().

[虛擬] QTimer:: ~QTimer ()

銷毀計時器。

template <typename Functor> QMetaObject::Connection QTimer:: callOnTimeout ( Functor slot , Qt::ConnectionType connectionType = Qt::AutoConnection)

這是重載函數。

創建連接,類型為 connectionType timeout () 信號到 slot ,並返迴要連接的句柄。

此方法為方便起見提供。它相當於調用 QObject::connect(timer, &QTimer::timeout, timer, slot, connectionType) .

該函數在 Qt 5.12 引入。

另請參閱 QObject::connect () 和 timeout ().

template <typename Functor> QMetaObject::Connection QTimer:: callOnTimeout (const QObject * context , Functor slot , Qt::ConnectionType connectionType = Qt::AutoConnection)

此函數重載 callOnTimeout()。

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

此方法為方便起見提供。它相當於調用 QObject::connect(timer, &QTimer::timeout, context, slot, connectionType) .

該函數在 Qt 5.12 引入。

另請參閱 QObject::connect () 和 timeout ().

template <typename MemberFunction> QMetaObject::Connection QTimer:: callOnTimeout (const QObject * receiver , MemberFunction * slot , Qt::ConnectionType connectionType = Qt::AutoConnection)

此函數重載 callOnTimeout()。

創建連接從 timeout () 信號到 slot receiver 對象。返迴連接的句柄。

此方法為方便起見提供。它相當於調用 QObject::connect(timer, &QTimer::timeout, receiver, slot, connectionType) .

該函數在 Qt 5.12 引入。

另請參閱 QObject::connect () 和 timeout ().

std::chrono::milliseconds QTimer:: intervalAsDuration () const

把此計時器的間隔返迴作為 std::chrono::milliseconds 對象。

該函數在 Qt 5.8 引入。

另請參閱 interval .

bool QTimer:: isActive () const

返迴 true 若計時器正在運行 (待決);否則返迴 false。

注意: getter 函數對於特性 active .

std::chrono::milliseconds QTimer:: remainingTimeAsDuration () const

把此計時器對象的剩餘時間返迴作為 std::chrono::milliseconds 對象。若此計時器到期 (或過期),返迴值為 std::chrono::milliseconds::zero() 。若找不到剩餘時間 (或計時器未激活),此函數返迴負持續時間。

該函數在 Qt 5.8 引入。

另請參閱 remainingTime ().

[static] void QTimer:: singleShot ( int msec , const QObject * receiver , const char * member )

此靜態函數調用槽,在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

範例:

#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTimer::singleShot(600000, &app, SLOT(quit()));
    ...
    return app.exec();
}
					

此範例程序在 10 分鍾 (600,000 毫秒) 後自動終止。

The receiver 是接收對象而 member 是槽。時間間隔為 msec 毫秒。

注意: 此函數是 可重入 .

另請參閱 setSingleShot () 和 start ().

[static] void QTimer:: singleShot ( int msec , Qt::TimerType timerType , const QObject * receiver , const char * member )

這是重載函數。

此靜態函數調用槽,在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

The receiver 是接收對象而 member 是槽。時間間隔為 msec 毫秒。 timerType 影響計時器精度。

注意: 此函數是 可重入 .

另請參閱 start ().

[static] template <typename PointerToMemberFunction> void QTimer:: singleShot ( int msec , const QObject * receiver , PointerToMemberFunction method )

這是重載函數。

此靜態函數調用成員函數對於 QObject 在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

The receiver 是接收對象而 method 是成員函數。時間間隔為 msec 毫秒。

receiver 被銷毀在間隔齣現前,方法不會被調用。函數將運行在綫程對於 receiver 。接收者綫程必須擁有正在運行的 Qt 事件循環。

注意: 此函數是 可重入 .

該函數在 Qt 5.4 引入。

另請參閱 start ().

[static] template <typename PointerToMemberFunction> void QTimer:: singleShot ( int msec , Qt::TimerType timerType , const QObject * receiver , PointerToMemberFunction method )

這是重載函數。

此靜態函數調用成員函數對於 QObject 在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

The receiver 是接收對象而 method 是成員函數。時間間隔為 msec 毫秒。 timerType 影響計時器精度。

receiver 被銷毀在間隔齣現前,方法不會被調用。函數將運行在綫程對於 receiver 。接收者綫程必須擁有正在運行的 Qt 事件循環。

注意: 此函數是 可重入 .

該函數在 Qt 5.4 引入。

另請參閱 start ().

[static] template <typename Functor> void QTimer:: singleShot ( int msec , Functor functor )

這是重載函數。

此靜態函數調用 functor 在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

時間間隔為 msec 毫秒。

注意: 此函數是 可重入 .

該函數在 Qt 5.4 引入。

另請參閱 start ().

[static] template <typename Functor> void QTimer:: singleShot ( int msec , Qt::TimerType timerType , Functor functor )

這是重載函數。

此靜態函數調用 functor 在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

時間間隔為 msec 毫秒。 timerType 影響計時器精度。

注意: 此函數是 可重入 .

該函數在 Qt 5.4 引入。

另請參閱 start ().

[static] template <typename Functor, int> void QTimer:: singleShot ( int msec , const QObject * context , Functor functor )

這是重載函數。

此靜態函數調用 functor 在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

時間間隔為 msec 毫秒。

context 被銷毀在間隔齣現前,方法不會被調用。函數將運行在綫程對於 context 。上下文綫程必須擁有正在運行的 Qt 事件循環。

注意: 此函數是 可重入 .

該函數在 Qt 5.4 引入。

另請參閱 start ().

[static] template <typename Functor, int> void QTimer:: singleShot ( int msec , Qt::TimerType timerType , const QObject * context , Functor functor )

這是重載函數。

此靜態函數調用 functor 在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

時間間隔為 msec 毫秒。 timerType 影響計時器精度。

context 被銷毀在間隔齣現前,方法不會被調用。函數將運行在綫程對於 context 。上下文綫程必須擁有正在運行的 Qt 事件循環。

注意: 此函數是 可重入 .

該函數在 Qt 5.4 引入。

另請參閱 start ().

[static] void QTimer:: singleShot ( std::chrono::milliseconds msec , const QObject * receiver , const char * member )

這是重載函數。

此靜態函數調用槽,在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

The receiver 是接收對象而 member 是槽。時間間隔被給齣由持續時間對象 msec .

注意: 此函數是 可重入 .

該函數在 Qt 5.8 引入。

另請參閱 start ().

[static] void QTimer:: singleShot ( std::chrono::milliseconds msec , Qt::TimerType timerType , const QObject * receiver , const char * member )

這是重載函數。

此靜態函數調用槽,在給定時間間隔後。

使用此函數非常方便,因為不需要麻煩采用 timerEvent 或創建本地 QTimer 對象。

The receiver 是接收對象而 member 是槽。時間間隔被給齣由持續時間對象 msec timerType 影響計時器精度。

注意: 此函數是 可重入 .

該函數在 Qt 5.8 引入。

另請參閱 start ().

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

這是重載函數。

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

若計時器已經在運行,它會被 stopped 並重啓。

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

該函數在 Qt 5.8 引入。

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

重實現: QObject::timerEvent (QTimerEvent *event).

int QTimer:: timerId () const

返迴計時器的 ID,若計時器正在運行;否則返迴 -1。