ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Qt:QTimer

2021-07-01 09:33:10  阅读:254  来源: 互联网

标签:Qt int interval start 计时器 QTimer


1、说明

QTimer类代表计时器,为了正确使用计时器,可以构造一个QTimer,将它的timeout()信号connect到合适的槽,之后调用start()。然后,这个QTimer就会每隔interval就发送一个timeout()信号。

一个间隔为1s(1000ms)的计时器

QTimer * timer = new QTimer(this);
connect(timer , &QTimer::timeout , this , QOverload<>::of(&AnalogClock::update));
tiimer->start(1000);

本例中,在start()计时器之后,update()槽函数将被每秒调用一次。

如果想要只超时一次,可以调用方法setSingleShot( true )。我们可以通过调用静态方法QTimer::singleShot()来在指定interval之后调用某个槽函数:

QTimer::singleShot(200,this,&Foo::updateCaption);

在多线程应用中,我们可以在每个使用事件循环(event looop)的线程中使用QTimer。使用QThread::exec()方法,可以从非GUI线程中start event loop。Qt使用计时器的 thread affinity(线程事务)去决定发送哪个timeout()信号。因此,只需要在一个线程中start或stop计时器就可以了。

 

2、模块和加载项

Header #include<QTimer>
qmake QT += core
Inherits QObject

3、属性

类型

属性

说明

getter与setter

信号

bool active 当计时器运行时,这个属性是true,否则是false  isActive()  
int interval 计时器间隔(毫秒)

 interval()

setInterval(int msec)

setInterval(std::chrono::milliseconds value)

 
int remainingTime 到计时器停止的剩余时间(毫秒) remainingTime()  
bool singleShot 该计时器是否是单响计时器

isSingleShot()

setSingleShot(bool singleShot)

 
TimerType timerType 控制计时器的精度

timerType()

setTimerType(Qt::TimerType atype)

 

4、构造

QTimer(QObject *parent = nullptr)

在给定的parent上构造计时器

5、成员方法

返回值类型

方法

说明

QMetaObject::Connection callOnTimeout(Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)  建立信号与槽之间的连接,等价于调用QObject::connect(timer , timeout() , receiver , slot , connectionType)
 callOnTimeout(const QObject *context, Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
callOnTimeout(const QObject *receiver, MemberFunction *slot, Qt::ConnectionType connectionType = Qt::AutoConnection 
 int interval()   

std::chrono::milliseconds

intervalAsDuration()   返回interval对应的std::chrono::milliseconds对象
 bool isActive()  当计时器运行时返回true,否则false
 bool remainingTime()   
std::chrono::milliseconds  remainingTimeAsDuration()   返回remaining time对应的std::chrono::milliseconds对象
 void     setInterval(int msec)  
setInterval(std::chrono::milliseconds value  
 setSingleShot(bool singleShot)  
setTimerType(Qt::TimerType atype  
 void start(int msec 重启一个interval为msec的计时器
 int timerId()  返回计时器的ID
 Qt::TimerType timerType()   

5.5、静态方法

void singleShot( ... )  设置一个单响计时器

 

6、槽

槽函数

说明

start() 启动计时器(如果已经启动则重启)
start(int msec) 重启一个间隔为msec的计时器 
stop() 停止计时器 

7、信号

timeout() 超时是发送该消息

标签:Qt,int,interval,start,计时器,QTimer
来源: https://www.cnblogs.com/ShineLeBlog/p/14956213.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有