Qt學習筆記:QTimer和QTime

來源:互聯網
上載者:User
QTimer Class

QTimer是一個計時器類
它的使用分三步,建立對象,串連signal和slot函數,start()

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

其中,SIGNAL(timeout())表示:每當計時結束,計時器歸零並重新計時,並發送一個訊號啟用slot函數。

而 timer->start(1000);當中的1000,就是1000毫秒的意思,表示每次timeout的時間間隔是1000ms

如果我們想讓這個計時器只計時一次,那麼必須使用void setSingleShot(bool singleShot)函數。

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

這樣計時器只會倒計時1分鐘,然後結束。

當然我們還可以改變計時周期

void setInterval(int msec)
QTime Class

QTime 提供時間函數給使用者使用,它和QTimer的區別就和手錶與秒錶的區別一樣。

QTime主要用於對時間的操作,他提供了大量的函數便於使用者對時間進行轉換和計算。

類型 名稱 說明
QTime() 構造一個時間為0的對象
QTime(int h, int m, int s = 0, int ms = 0) 構造一個具有初始時間的對象
QTime addMSecs(int ms) const 在目前時間基礎上增加ms毫秒,ms可為負
QTime addSecs(int s) const 在目前時間基礎上增加s秒,s可為負
int hour() const 返回小時數
int minute() const 返回分鐘數
int second() const 返回秒
int msec() const 返回毫秒
bool isValid() const 判斷當前對象的時間是否有效,畢竟1天不可能有25小時,也不會存在1分61秒
bool isValid(int h, int m, int s, int ms = 0) 判斷輸入的時間是否有效
int msecsTo(const QTime & t) const 計算距離時間t的毫秒數,如果t早於目前時間,則為負
int secsTo(const QTime & t) const 計算距離時間t的秒數
bool setHMS(int h, int m, int s, int ms = 0) 設定標準HMS時間,如果不符合標準,返回false
下面是最重要的幾個
void start() 將當前系統時間記錄為目前時間
int restart() 將當前系統時間記錄為目前時間,並返回距離上次呼叫start()或者restart()函數間隔的毫秒數
int elapsed() const 計算與最近一次呼叫start()或者restart()函數間隔的毫秒數,相當於計時器
QString toString(const QString & format) const 將時間轉化為特定的字串格式
QString toString(Qt::DateFormat format = Qt::TextDate) const 按照Qt::DateFormat的格式轉化
QTime fromString(const QString & string, Qt::DateFormat format = Qt::TextDate) 從Qt::DateFormat轉化為QTime對象
QTime fromString(const QString & string, const QString & format) 從特定的字串格式轉化為QTime對象
QTime currentTime() 得到當前的系統時間

此外,const QString & format需要特別說明,表格如下:

Expression Output
h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H the hour without a leading zero (0 to 23, even with AM/PM display)
HH the hour with a leading zero (00 to 23, even with AM/PM display)
m the minute without a leading zero (0 to 59)
mm the minute with a leading zero (00 to 59)
s the second without a leading zero (0 to 59)
ss the second with a leading zero (00 to 59)
z the milliseconds without leading zeroes (0 to 999)
zzz the milliseconds with leading zeroes (000 to 999)
AP interpret as an AM/PM time. AP must be either “AM” or “PM”.
ap Interpret as an AM/PM time. ap must be either “am” or “pm”.
t the timezone (for example “CEST”)

例子:

Format Result
hh:mm:ss.zzz 14:13:09.042
h:m:s ap 2:13:9 pm
H:m:s a 14:13:9 pm

而Qt::DateFormat又分為很多種,比如Qt::TextDate、Qt::ISODate等,詳請見官方說明,這裡就不一一指出了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.