打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Qt之QTimer和QTime
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)构造一个具有初始时间的对象
QTimeaddMSecs(int ms) const在当前时间基础上增加ms毫秒,ms可为负
QTimeaddSecs(int s) const在当前时间基础上增加s秒,s可为负
inthour() const返回小时数
intminute() const返回分钟数
intsecond() const返回秒
intmsec() const返回毫秒
boolisValid() const判断当前对象的时间是否有效,毕竟1天不可能有25小时,也不会存在1分61秒
boolisValid(int h, int m, int s, int ms = 0)判断输入的时间是否有效
intmsecsTo(const QTime & t) const计算距离时间t的毫秒数,如果t早于当前时间,则为负
intsecsTo(const QTime & t) const计算距离时间t的秒数
boolsetHMS(int h, int m, int s, int ms = 0)设置标准HMS时间,如果不符合标准,返回false
下面是最重要的几个
voidstart()将当前系统时间记录为当前时间
intrestart()将当前系统时间记录为当前时间,并返回距离上次呼叫start()或者restart()函数间隔的毫秒数
intelapsed() const计算与最近一次呼叫start()或者restart()函数间隔的毫秒数,相当于计时器
QStringtoString(const QString & format) const将时间转化为特定的字符串格式
QStringtoString(Qt::DateFormat format = Qt::TextDate) const按照Qt::DateFormat的格式转化
QTimefromString(const QString & string, Qt::DateFormat format = Qt::TextDate)从Qt::DateFormat转化为QTime对象
QTimefromString(const QString & string, const QString & format)从特定的字符串格式转化为QTime对象
QTimecurrentTime()得到当前的系统时间
此外,const QString & format需要特别说明,表格如下:
ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
Hthe hour without a leading zero (0 to 23, even with AM/PM display)
HHthe hour with a leading zero (00 to 23, even with AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
APinterpret as an AM/PM time. AP must be either “AM” or “PM”.
apInterpret as an AM/PM time. ap must be either “am” or “pm”.
tthe timezone (for example “CEST”)
例子:
FormatResult
hh:mm:ss.zzz14:13:09.042
h:m:s ap2:13:9 pm
H:m:s a14:13:9 pm
而Qt::DateFormat又分为很多种,比如Qt::TextDate、Qt::ISODate等
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:
Qt串口助手(1)
Qt对定时及时间系统的处理
关于Qt中常用的延时方法
Qt中定时器使用的两种方法
Qt之QTimer
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服