Qt 利用QTime类来控制时间,QTime的成员函数的用法

QTime::QTime()
默认构造函数,构造一个时,分,秒都为0的时间,如00:00:00.000(午夜)

QTime::QTime(int h, int m, int s=0, int ms = 0)
构造一个用户指定时,分,秒的时间.
其参数有效值为:
h:0--23
m:0--59
ms:0--999

QTime QTime::addMSecs(int ms) const
返回一个当前时间对象之后或之前ms毫秒的时间对象(之前还是之后视ms的符号,如为正则之后,反之之前)
如:QTime time(3,0,0);
   QTime newTime1 = time.addMSecs(1000);
   QTime newTime2 = time.addMSecs(-1000);
则newTime1是一个比time所指定时间(03:00:00.000)延后1000毫秒也即1秒的时间(03:00:01.000),而newTime2则提前1000毫秒(02:59:59.000)

QTime QTime::addSecs(int nsecs) const
与addMSecs()相同,只是nsecs单位是秒.即返回一个当前时间对象之前或之后的时间对象.

int QTime::elapsed() const
返回最后一次调用start()或restart()到现在已经经过的毫秒数.如果经过了24小时之后,则计数器置0.如果系统时间设置改变,则结果不确定.

int QTime::hour() const
返回时间对象的小时,取值范围(0--23)

int QTime::minute() const
返回时间对象的分钟,取值范围(0--59)

int QTime::second() const
返回时间对象的秒,取值范围(0--59)

int QTime::msec() const
返回时间对象的毫秒,取值范围(0--999)

bool QTime::isNull() const
如果时间对象等于00:00:00.000,则返回true;反之返回false.

bool QTime::isValid() const
如果时间对象是有效的,则返回true;反之返回false.(即:时,分,秒,毫秒都在其取值范围之内)

int QTime::msecsTo(const QTime &t) const
返回当前时间对象到t所指定的时间之间的毫秒数.如果t早于当前时间对象的时间,则返回的值是负值.因为一天的时间是86400000毫秒,所以返回值范围是-86400000--86400000

int QTime::secsTo(const QTime &t) const
与msecsTo()基本相同,只是返回的是秒数,返回值的有效范围是-86400--86400

int QTime::restart()
设置当前时间对象的值为当前系统时间,并且返回从最后一次调用start()或restart()到现在的毫秒数.如果计数器超出24小时,则置0.如果计数器计数时系统时间设置改变,则结果不确定.

bool QTime::setHMS(int h, int m, int s, int ms = 0)
设置当前时间对象的时,分,秒和毫秒.如果给定的参数值有效,则返回true,否则返回false.

void QTime::start()
设置当前时间对象的值为当前系统时间,这个函数实际是结合restart()和elapsed()用来计数的.

QString QTime::toString(const QString &format) const
按照参数format指定的格式用字符串形式输出当前时间对象的时间.
参数format用来指定时,分,秒,毫秒的输出格式.如(hh:mm:ss.zzz)
h:表示小时,范围是0--23
hh:用两位数表示小时,不足两位的前面用0补足,如(0点:00,3点:03,11点:11)
m:表示分钟,范围0--59
mm:用两位数表示分钟,不足两位的前面用0补足.
s:表示秒,范围0--59
ss:用两位数表示秒,不足两位的前面用0补足.
z:表示毫秒,范围0--999
zzz:用三位数表示毫秒,不足三位的前面用0补足.
AP:用AM/PM显示.
ap:用ap/pm显示.
例如:
QTime time(14,3,9,42);//设置时间为14:03:09.042
QString i = time.toString("hh:mm:ss.zzz");//结果为14:03:09.042
QString j = time.toString("h:m:s.z");//结果为14:3:9.42
QString m = time.toString("h:m:s.z AP");//结果为2:3:9.42 PM
QString n = time.toString("h:m:s.z ap");//结果为2:3:9.42 pm

QString QTime::toString(Qt::DateFormat f = Qt::TextDate) const
按照参数format指定的格式用字符串形式输出当前时间对象的时间.
参数的可选值:
Qt::TextDate:格式为HH:MM:SS
Qt::ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SS
Qt::LocalDate:字符串格式依赖系统本地设置

----------------------------------------------------------------------------------------------------------------------------------------

静态成员函数:

QTime QTime::currentTime()
返回当前的系统时间.

QTime QTime::fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
使用参数format指定的格式根据参数string指定的时间返回一个时间对象。如果string指定的时间不合法,则返回一个无效的时间对象。
format可选值:
Qt::TextDate:格式为HH:MM:SS
Qt::ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SS
Qt::LocalDate:字符串格式依赖系统本地设置

QTime QTime::fromString(const QString &string, const QString &format)
使用参数format指定的格式根据参数string指定的时间返回一个时间对象.如果string指定的时间不合法,则返回一个无效的时间对象.
format的格式参看QString QTime::toString(const QString &format) const.

bool QTime::isValid(int h, int m, int s, int ms = 0)
如果参数所指定的时间是合法的,则返回true;反之返回false.

----------------------------------------------------------------------------------------------------------------------------------------

静态成员函数不依赖于对象,可以通过类直接调用,与对象无关:

如:获取当前系统时间的小时部分时不需要定义QTime对象

int hour = QTime::currentTime().hour()

原文地址:https://www.cnblogs.com/ybqjymy/p/14666548.html