使用QT,如何在一定间隔后调用函数一次,即使可能发生更多的调用? [英] Using QT, how to call function once after a certain interval, even if more calls may occur?

查看:1048
本文介绍了使用QT,如何在一定间隔后调用函数一次,即使可能发生更多的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难说这个问题,即使我不认为它那么复杂。

I am having a hard time wording this question even though I don't think its that complicated.

我想对 QTimer :: singleshot()执行一些操作,但我希望它仍然只调用即使 QTimer :: singleshot()在其被触发之前被多次调用,也会执行SLOT一次。

I want to do something simalar to QTimer::singleshot() but I want it to still only call the SLOT once even if QTimer::singleshot() is called multiple times before it fires.

推荐答案

这应该可以工作。

class MyObject
{

// ...
    QTimer* mTimer;
}

MyObject::MyObject()
{
    mTimer = new QTimer(this);
    mTimer->setSingleShot(true);
    connect(mTimer, SIGNAL(timeout()), SLOT(doStuff()));
}

MyObject::startOrResetTimer()
{
   mTimer->start(1000);
}

这篇关于使用QT,如何在一定间隔后调用函数一次,即使可能发生更多的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆