sm11963 sm11963 - 3 months ago 8
C++ Question

Using QT, how to call function once after a certain interval, even if more calls may occur?

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

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.

Answer

This should work. 

class MyObject
{

// ...
    QTimer* mTimer;
}

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

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