Qt 定时器之 singleShot 用法

[static] void QTimer::singleShot(int msec, const QObject *receiver, const char *member)

这个静态函数在一个给定时间间隔 msec(毫秒) 之后调用一个槽。

用法1 :
假设类A有个槽函数 function() { }
我们要在10s之后执行它
就可以: QTimer::singleShot(10*1000,this, &A::function());

用法2:
实现循环
槽函数中还是singleShot 即:

    function(){
        QTimer::singleShot(10*1000,this, &A::function());
    }

但是为了让循环可控,应该加个条件

bool condition = true;
function(){
    if(condition){
        QTimer::singleShot(10*1000,this, &A::function());
    }
}
  • 18
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
QTimer的singleShot函数是Qt提供的一种实现单次定时器的方法。使用这个函数可以在指定的时间间隔之后调用槽函数。它具有简洁、方便的特点,推荐使用。 使用singleShot函数的方法有两种: 1. 使用静态接口,调用QTimer::singleShot函数。示例代码如下: QTimer::singleShot(1000, this, SLOT(slot_Timerout())); QTimer::singleShot(1000, slot_Timerout); // 参数不同,但是实现方式相同 2. 使用QTimer对象的singleShot接口。示例代码如下: QTimer *pTimer = new QTimer(this); pTimer->singleShot(10000, slot_Timerout); 可以看出,使用单次定时器的方法较为简单。根据具体需求选择合适的方法即可。同时,QTimer::singleShot函数的参数解释如下:第一个参数是延迟的时间(以毫秒为单位),第二个参数是接收器对象(用于指定槽函数所属的对象),第三个参数是槽函数的名称。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Qt之单次定时器](https://blog.csdn.net/weixin_43519792/article/details/107304506)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Qt 定时器singleShot 用法](https://blog.csdn.net/kidults/article/details/80091788)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值