Which is the correct way to create a pause/waiting in QT?
-
Hi all,
I want to create a pause/waiting to pause current thread while still allow other threads to process their events. But I am not sure which way below is correct.
QTime t;
t.start();
while (t.elapsed() < 1000)
{
QCoreApplication::processEvents();
}OR
QCoreApplication::processEvents();
QThread::msleep(1000);So anyone can help me?
Please let me know if you have better solution.Thanks!
-
When it's a separate thread and not the gui thread then QThread::msleep(1000); is ok - otherwise I simply would use a singleshot QTimer.
-
Hi,
Neither is a good idea as you are not waiting for your other threads to finish you are just "sleeping".
What exactly are you waiting on ?
Depending on that, you might want to consider modeling your system with a state machine. -
When it's a separate thread and not the gui thread then QThread::msleep(1000); is ok - otherwise I simply would use a singleshot QTimer.
@christian-ehrlicher
Got it, thanks! -
@sgaist
Yeah, you're right. Actually I want to wait for other threads to finish first, then continue to do something in current thread. But I have no idea about how to know the other threads have finished or not, so I just set a sleeping time by experience. -
@sgaist
Yeah, you're right. Actually I want to wait for other threads to finish first, then continue to do something in current thread. But I have no idea about how to know the other threads have finished or not, so I just set a sleeping time by experience. -
@ericliii Emit a signal from the other threads when they are finished. Connect that signal to a slot in the main thread, and handle the finished state there.
Regards
-
@ericliii
Although @aha_1980 tells you to "Emit a signal from the other threads when they are finished", I would have thought that the existing https://doc.qt.io/qt-5/qthread.html#finished signal does just that for you? Having said that, have a read of https://stackoverflow.com/questions/17045368/qthread-emits-finished-signal-but-isrunning-returns-true-and-isfinished-re accepted solution, and https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ -
@ericliii
Although @aha_1980 tells you to "Emit a signal from the other threads when they are finished", I would have thought that the existing https://doc.qt.io/qt-5/qthread.html#finished signal does just that for you? Having said that, have a read of https://stackoverflow.com/questions/17045368/qthread-emits-finished-signal-but-isrunning-returns-true-and-isfinished-re accepted solution, and https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/@jonb
Thanks for you answer!void QThread::finished() is useful but not what I exactly want. I just need to wait for a specific event in another thread to finish. No need to sleep until the whole thread is finished. So I think signal-slot is a better choice.
-
@jonb
Thanks for you answer!void QThread::finished() is useful but not what I exactly want. I just need to wait for a specific event in another thread to finish. No need to sleep until the whole thread is finished. So I think signal-slot is a better choice.
-
@ericliii
void QThread::finished()
is already just as much "signal-slot" as your own explicit one, just saying! But yes emit your own signal if you want to know earlier than thread finish.