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 }
原文地址:https://www.cnblogs.com/ybqjymy/p/14666542.html