PDA

View Full Version : are there functions for delay in Qt?



rezas1000
26th September 2014, 16:10
Hello. in standard c++, there are functions for delay such as delay(int msec).I want to know that are there functions for delay in Qt? thank you.

anda_skoa
26th September 2014, 16:51
Well, if they are in standard C++, then they obviously have to be usable in Qt applications, no?

Cheers,
_

rezas1000
26th September 2014, 19:14
:) , Like it, is it possible with QThread? for example: QThread::sleep(unsigned int)

anda_skoa
26th September 2014, 20:25
Is there anything you actually do yourself?
Like checking the documentation, or $DEITY forbid, actually trying it?

Cheers,
_

rezas1000
26th September 2014, 21:15
I saw the documentation and In fact, QThread::sleep() or QThread::msleep() are for delay operation.What do you think? if this is wrong please correct.thank you.

stampede
26th September 2014, 21:41
Yes those functions can be used to suspend a thread for a given interval. Why do you need that ?
Maybe a single shot timer would be better ? Forcing a thread to sleep is rarely needed.

ChrisW67
26th September 2014, 21:44
Anda_skoa is merely expressing that, even though you found these in the docs, you would rather ask this forum to verfy things for you rather than simply trying it yourself. Quite often the question takes longer to type than trying it yourself would have.

Yes, those functions exist in QThread, and yes they cause blocking delays. Blocking delays are usually not what you want in a Qt program (and they usually lead newbies to think they need threads further confusing the issue). For the usual Qt way of having something happen in x milliseconds see QTimer.

magnetron
25th June 2018, 09:15
Blocking delay:



void ClassA::msleep(int msec)
{
QThread::msleep(msec);
}


Non-blocking delay:



void ClassA::msleep(int msec)
{
QEventLoop loop;

QTimer::singleShot(msec, &loop, &QEventLoop::quit);

loop.exec();
}