Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:

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()

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt是一个跨平台的C++应用程序开发框架,它提供了丰富的类库和工具,可以用于开发图形界面应用程序、网络应用程序、数据库应用程序等。QTimeQt的一个类,用于处理时间相关的操作。 QTime类提供了一些方法来获取和操作时间,包括获取当前时间、设置时间、计算时间差等。下面是一些常用的QTime类的方法: 1. `QTime::currentTime()`:获取当前时间。 2. `QTime::setHMS(int hour, int minute, int second)`:设置时间的小时、分钟和秒。 3. `QTime::hour()`、`QTime::minute()`、`QTime::second()`:获取时间的小时、分钟和秒。 4. `QTime::addSecs(int seconds)`:增加指定的秒数。 5. `QTime::secsTo(const QTime &time)`:计算当前时间与指定时间之间的秒数差。 6. `QTime::toString(const QString &format)`:将时间转换为字符串,可以指定格式。 以下是一个使用QTime的示例代码: ```cpp #include <QCoreApplication> #include <QDebug> #include <QTime> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 获取当前时间 QTime currentTime = QTime::currentTime(); qDebug() << "Current time: " << currentTime.toString("hh:mm:ss"); // 设置时间为12:30:45 QTime customTime; customTime.setHMS(12, 30, 45); qDebug() << "Custom time: " << customTime.toString("hh:mm:ss"); // 增加10秒 customTime = customTime.addSecs(10); qDebug() << "Custom time after adding 10 seconds: " << customTime.toString("hh:mm:ss"); // 计算时间差 int secondsDiff = currentTime.secsTo(customTime); qDebug() << "Seconds difference: " << secondsDiff; return a.exec(); } ``` 运行以上代码,你将会看到输出的当前时间、设置的自定义时间、增加秒数后的时间以及时间差。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值