1. 程式人生 > >Qt入門 Qt中時間設定(五)

Qt入門 Qt中時間設定(五)

Qtimer

QTimer類提供了重複和單次觸發訊號的定時器。

重複觸發 - 時鐘

QTimer類為定時器提供了一個高級別的程式設計介面。很容易使用:首先,建立一個QTimer,連線timeout()訊號到適當的槽函式,並呼叫start(),然後在恆定的時間間隔會發射timeout()訊號。

利用Qtimer,可以很輕鬆的模擬出一個時鐘:

QTimer* timer = new QTimer(this);
//若當前時間超時,過了1000ms,更新m_pzEffectWidget視窗
connect(timer, SIGNAL(timeout()), m_pzEffectWidget, SLOT(update()));
timer->start(1000
);

單次觸發

使用setSingleShot(true)

QTimer *timer = new QTimer(this);
timer->setSingleShot(true);//設定為true之後,就會只觸發一次
//過1000ms即觸發一次
connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
timer->start(1000);

使用靜態函式QTimer::singleShot()

//過200ms觸發
QTimer::singleShot(200, this, SLOT(updateCaption()));