QTimer 類

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

頭: #include <QTimer>
繼承: QObject

特性

公共函數

QTimer (QObject * parent = 0)
~QTimer ()
int interval () const
bool isActive () const
bool isSingleShot () const
void setInterval (int msec )
void setSingleShot (bool singleShot )
int timerId () const

公共槽

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

信號

void timeout ()

靜態公共成員

void singleShot (int msec , QObject * receiver , const char * member )

重實現保護函數

virtual void timerEvent (QTimerEvent * e )

詳細描述

The QTimer 類提供重復 (和單發) 計時器。

The QTimer 類為計時器提供高級編程接口。要使用它,創建 QTimer ,連接其 timeout () 信號到適當槽,並調用 start (). From then on it will emit the timeout () 信號按常量間隔。

一秒 (1000 毫秒) 計時器範例 (來自 指針式時鍾 範例):

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(1000);
					

從那時起, update() 槽被每秒調用。

可以將計時器設為僅超時一次,通過調用 setSingleShot (true)。也可以使用靜態 QTimer::singleShot () 函數調用槽在指定間隔後:

    QTimer::singleShot(200, this, SLOT(updateCaption()));
					

在多綫程應用程序中,可以使用 QTimer 在擁有事件循環的任何綫程中。要從非 GUI 綫程啓動事件循環,使用 QThread::exec ()。Qt 使用計時器的 綫程親緣關係 確定哪個綫程將發射 timeout() 信號。因此,必須在其綫程中啓動和停止計時器;從另一綫程啓動計時器,是不可能的。

作為特殊情況, QTimer 采用 0 超時將盡快超時,在已處理窗口係統事件隊列中的所有事件後。可以使用這做繁重工作,當提供敏捷用戶界麵時:

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
    timer->start();
					

processOneThing() will from then on be called repeatedly. It should be written in such a way that it always returns quickly (typically after processing one data item) so that Qt can deliver events to widgets 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; multithreading is now becoming available on more and more platforms, and we expect that zero-millisecond QTimers will gradually be replaced by QThread

精度和計時器分辨率

Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.

計時器的準確性取決於底層操作係統和硬件。大多數平颱支持 1 毫秒的分辨率,雖然計時器的準確性在許多真實世界狀況下不會等於此分辨率。

If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.

替代 QTimer

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

Another alternative to using QTimer is to use QBasicTimer 。通常不那麼麻煩相比使用 QObject::startTimer () 直接。見 計時器 瞭解所有 3 種途徑的概述。

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

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

特性文檔編製

active : const bool

This boolean property is true if the timer is running; otherwise false.

該特性在 Qt 4.3 引入。

訪問函數:

bool isActive () const

interval : int

This property holds the timeout interval in milliseconds.

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

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

訪問函數:

int interval () const
void setInterval (int msec )

另請參閱 singleShot .

singleShot : bool

This property holds whether the timer is a single-shot timer.

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

訪問函數:

bool isSingleShot () const
void setSingleShot (bool singleShot )

另請參閱 interval and singleShot ().

成員函數文檔編製

QTimer:: QTimer ( QObject * parent = 0)

構造計時器采用給定 parent .

QTimer:: ~QTimer ()

銷毀計時器。

[static] void QTimer:: singleShot ( int msec , 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 ().

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

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

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

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

[slot] void QTimer:: start ()

此函數重載 start ().

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

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

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

[slot] void QTimer:: stop ()

停止計時器。

另請參閱 start ().

[signal] void QTimer:: timeout ()

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

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

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

重實現自 QObject::timerEvent ().

int QTimer:: timerId () const

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