前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qt学习笔记#4:QTimer和QTime

Qt学习笔记#4:QTimer和QTime

作者头像
全栈程序员站长
发布2022-07-31 17:04:44
8050
发布2022-07-31 17:04:44
举报

大家好,又见面了,我是你们的朋友全栈君。

文章目录

QTimer Class

QTimer是一个计时器类 它的使用分三步,创建对象,连接signal和slot函数,start()

代码语言:javascript
复制
	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)函数。

代码语言:javascript
复制
	QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->setsetSingleShot(true)
    timer->start(60000);

这样计时器只会倒计时1分钟,然后结束。

当然我们还可以改变计时周期

代码语言:javascript
复制
void setInterval(int msec)

QTime Class

QTime 提供时间函数给用户使用,它和QTimer的区别就和手表与秒表的区别一样。

QTime主要用于对时间的操作,他提供了大量的函数便于用户对时间进行转换和计算。

类型

名称

说明

QTime()

构造一个时间为0的对象

QTime(int h, int m, int s = 0, int ms = 0)

构造一个具有初始时间的对象

QTime

addMSecs(int ms) const

在当前时间基础上增加ms毫秒,ms可为负

QTime

addSecs(int s) const

在当前时间基础上增加s秒,s可为负

int

hour() const

返回小时数

int

minute() const

返回分钟数

int

second() const

返回秒

int

msec() const

返回毫秒

bool

isValid() const

判断当前对象的时间是否有效,毕竟1天不可能有25小时,也不会存在1分61秒

bool

isValid(int h, int m, int s, int ms = 0)

判断输入的时间是否有效

int

msecsTo(const QTime & t) const

计算距离时间t的毫秒数,如果t早于当前时间,则为负

int

secsTo(const QTime & t) const

计算距离时间t的秒数

bool

setHMS(int h, int m, int s, int ms = 0)

设置标准HMS时间,如果不符合标准,返回false

下面是最重要的几个

void

start()

将当前系统时间记录为当前时间

int

restart()

将当前系统时间记录为当前时间,并返回距离上次呼叫start()或者restart()函数间隔的毫秒数

int

elapsed() const

计算与最近一次呼叫start()或者restart()函数间隔的毫秒数,相当于计时器

QString

toString(const QString & format) const

将时间转化为特定的字符串格式

QString

toString(Qt::DateFormat format = Qt::TextDate) const

按照Qt::DateFormat的格式转化

QTime

fromString(const QString & string, Qt::DateFormat format = Qt::TextDate)

从Qt::DateFormat转化为QTime对象

QTime

fromString(const QString & string, const QString & format)

从特定的字符串格式转化为QTime对象

QTime

currentTime()

得到当前的系统时间

此外,const QString & format需要特别说明,表格如下:

Expression

Output

h

the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)

hh

the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)

H

the hour without a leading zero (0 to 23, even with AM/PM display)

HH

the hour with a leading zero (00 to 23, even with AM/PM display)

m

the minute without a leading zero (0 to 59)

mm

the minute with a leading zero (00 to 59)

s

the second without a leading zero (0 to 59)

ss

the second with a leading zero (00 to 59)

z

the milliseconds without leading zeroes (0 to 999)

zzz

the milliseconds with leading zeroes (000 to 999)

AP

interpret as an AM/PM time. AP must be either “AM” or “PM”.

ap

Interpret as an AM/PM time. ap must be either “am” or “pm”.

t

the timezone (for example “CEST”)

例子:

Format

Result

hh:mm:ss.zzz

14:13:09.042

hⓂ️s ap

2:13:9 pm

HⓂ️s a

14:13:9 pm

而Qt:

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127665.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • QTimer Class
  • QTime Class
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档