Qt学习笔记#4:QTimer和QTime

QTimerClassQTimer是一个计时器类它的使用分三步,创建对象,连接signal和slot函数,start()QTimer*timer=newQTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(update()));timer->start(1000);其中,SIGNAL(timeou

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

文章目录


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) 构造一个具有初始时间的对象
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::DateFormat又分为很多种,比如Qt::TextDate、Qt::ISODate等,详请见官方说明,这里就不一一指出了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python操作Elasticsearch数据库[通俗易懂]

    python操作Elasticsearch数据库[通俗易懂]fromelasticsearchimportElasticsearch#连接数据库es=Elasticsearch([{‘host’:”localhost”,’port’:9200}])#创建索引es.indices.create(index=’es_python’,ignore=400)#插入单条数据es.index(index=”es_python”,doc_type=…

    2022年5月1日
    57
  • AVC1与H264的区别

    AVC1与H264的区别今天上网时偶尔发现这个在我脑海里疑惑的问题的答案。H.264VideoTypesThefollowingmediasubtypesaredefinedforH.264video.SubtypeFOURCCDescri

    2022年6月17日
    44
  • net start命令发生系统错误5和错误1058的解决方法

    net start命令发生系统错误5和错误1058的解决方法文章来源:1.netstart命令netstart命令用于开启服务,格式为:netstart[服务名]2.打开SqlServer的默认实例服务[Win+R]快捷键—>cmd—>netstartmssqlserver提示错误“发生系统错误5”,截图如下:3.错误分析发生这个错误的原因是win7/win8命令提示符管理员和非管理员权限有区别,需要

    2022年7月26日
    1
  • Redis客户端工具_redis安装教程

    Redis客户端工具_redis安装教程作者:IT邦德中国DBA联盟(ACDU)成员,目前从事DBA及程序编程(Web\java\Python)工作,主要服务于生产制造现拥有Oracle11gOCP/OCM、Mysql、Oceanbase(OBCA)认证分布式TBase\TDSQL数据库、国产达梦数据库以及红帽子认证从业8年DBA工作,在数据库领域有丰富的经验B站主播Oracle、Mysql、PG实战课程,请搜索:jeames007前言RedisDesktopManager官方版是一款简单快速、跨平台的Redi

    2022年9月19日
    0
  • SMP技术_sls技术最重要的是使用领域是

    SMP技术_sls技术最重要的是使用领域是SMP英文全称为SymmetricalMulti-Processing,意指“对称多处理”技术,是指在一个计算机上汇集了一组处理器—即多CPU,各CPU之间共享内存子系统以及总线结构。它是相对非对称多处理技术而言的、应用十分广泛的并行技术。在这种架构中,一台电脑不再由单个CPU组成,而同时由多个处理器运行操作系统的单一复本,并共享内存和一台计算机的其他资源。虽然同时使用多个CPU,但是从管理的角度来看,它们的表现就像一台单机一样。系统将任务队列对称地分布于多个CPU之上,从而极大地提高了整个系统的数据处理

    2022年10月27日
    0
  • ubuntu安装nginx及其依赖「建议收藏」

    ubuntu安装nginx及其依赖「建议收藏」最近安装nginx踩了一些坑,所以在这记录一下,也希望能够帮助到一些人。下面就开始说一下我的安装步骤:一:安装pcre:打开终端,输入以下命令:sudo-s#进入root用户aptinstallbuild-essential#安装gcc编译器及其环境,包含gcc,gdb,make等wgethttps://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz#下载安装包tar-zxvfpcre-8.37.tar.gz#解压cd

    2022年9月19日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号