Qt QTime类的使用
The QTime class provides clock time functions.
A QTime object contains a clock time, i.e. the number of hours, minutes, seconds, and milliseconds since midnight. It can read the current time from the system clock and measure a span of elapsed time. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds.
QTime uses the 24-hour clock format; it has no concept of AM/PM. Unlike QDateTime, QTime knows nothing about time zones or daylight-saving time (DST).
A QTime object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static function currentTime(), which creates a QTime object that contains the system's local time. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
The hour(), minute(), second(), and msec() functions provide access to the number of hours, minutes, seconds, and milliseconds of the time. The same information is provided in textual format by the toString() function.
QTime provides a full set of operators to compare two QTime objects. QTime A is considered smaller than QTime B if A is earlier than B.
The addSecs() and addMSecs() functions provide the time a given number of seconds or milliseconds later than a given time. Correspondingly, the number of seconds or milliseconds between two times can be found using secsTo() or msecsTo().
QTime can be used to measure a span of elapsed time using the start(), restart(), and elapsed() functions.
See also QDate and QDateTime
 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include <QTime>
 4 #include <QDebug>
 5 #include <windows.h>
 6 MainWindow::MainWindow(QWidget *parent) :
 7     QMainWindow(parent),
 8     ui(new Ui::MainWindow)
 9 {
10     ui->setupUi(this);
11  
12     QTime time = QTime::currentTime();//獲取當前事件
13     QString strTime = time.toString("hh:mm:ss.zzz");//轉換成QString
14     time = QTime::fromString("02:23:45.789","hh:mm:ss.zzz");//QString轉QTime
15     strTime = time.toString("hh:mm:ss.zzz");
16     QTime time1(7,30,5 ,100);//傳遞時分秒毫秒構造函數
17     strTime = time1.toString("hh:mm:ss.zzz");
18     int hour = time1.hour();//獲取小時,7
19     qDebug()<<hour;
20     int minute=time1.minute();//獲取分鐘,30
21     qDebug()<<minute;
22     int second = time1.second();//獲取秒數,5
23     qDebug()<<second;
24     int mssecond = time1.msec();//獲取毫秒,100
25     qDebug()<<mssecond;
26     QTime t1 = time1.addMSecs(100);//增加100ms
27     qDebug()<<t1.msec();
28     QTime t2 = time1.addSecs(80);//增加80s
29     qDebug()<<t2.second();
30     if(time < time1)//比較兩個QTime,時間晚的大
31         qDebug()<<"time<time1";
32     QTime t3;
33     t3.start();//開始計時
34     Sleep(1000);
35     int elspsed = t3.elapsed();//計算多少ms過去了
36     t3.restart();//重新計時
37     int milsecond = QTime::currentTime().msecsSinceStartOfDay();//返回從零點開始共計多少ms
38     qDebug()<<milsecond;
39     QTime t4(22,25,10);
40     QTime t5(22,24,0);
41     qDebug()<<t4.secsTo(t5);//兩者相差了多少秒,t4到t5需要多少秒,如果t5<t4,返回負值
42     qDebug()<<strTime;
43 }
44  
45 MainWindow::~MainWindow()
46 {
47     delete ui;
48 }
                            總結
以上是生活随笔為你收集整理的Qt QTime类的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        