Issue
I have a PyQt GUI where the user presses a button to start a background thread (workerThread, which is subclassed from QThread). I would like to have a timer display (in the form of a QLabel) to show how much time has elapsed since workerThread started, and I would like this timer to stop as soon as the workerThread exits.
Possible solution
I’ve thought about creating another independent thread (timerThread) that uses a QTimer to send a signal to a slot to update the QLabel in the main GUI thread with the elapsed time every 1 second. This timerThread will exit as soon as it receives a terminate signal from workerThread.
However, I’d have to start timerThread at the same time as WorkerThread, and I’m not sure how to this.
Question
Is there an easier way to do this? Is QTimer even the right approach to start with?
Here is one way of doing this. In this example,
myThreadstarts thetimerThreadas a child process when it’srunmethod is called. It’stimeElapsedsignal is connected to thetimerThreadstimeElapsedsignal. ThetimerThreadwill check each secondwhiletheparentisRunningand emit atimeElapsedsignal ifTrue: