Let's say I have a PyQt GUI application and a QTimer running in the background using a QThread. The QTimer is set to an interval of 1 second and is running infinitely.
Now the main thread gets busy with some external C library call and blocks for 1 minute. Will the QTimer continue to get triggered during that period and signal it?
I am in a conceptual stage so there is no code to show yet. I know it would make more sense to do the big call in a background thread itself.
In short words, yes, QTimer will update on the other thread, granted that you take care of the thread's lifespan yourself and quit it only at the most appropriate times. Also be careful about the
QTimer'sreference, so it is not collected by the python's garbage collector (you'll need to stop theQTimerat one point).Now, with a bit of code to illustrate a more practical example, and so you can test it yourself:
In this example, we start a
QThreadwhich executesTimerWorker.run()on the other thread (using the moveToThread method). At each 0.25 seconds, it emits a signal that is scheduled on the main thread, to update theQLabeltext.At the same time, the user can click a
QPushButtonto stop the main thread for 5 seconds. During that time, the application will freeze.By executing the script and clicking on the
QPushButtonto freeze the main thread, theQTimerkeeps running and emitting the signal from the other thread. But as the main thread is sleeping (or busy), the signal is never processed. The signal will only be processed after the main thread awakes.So, if you need to keep the GUI always responsive for the Client (and always processing emitted signals), its recomended that you do all the heavy work on the
QThreadside, to not keep the application frozen for a time.