So I know there are a lot of posts about updating elements in a GUI with Qthread. I did my best to go over these, but still have a question.
I\'m trying to create a
There are a few things wrong with your code, however you were pretty close.
The first obvious one is that you have a loop with a time.sleep()
in your main thread. The Ui_Dialog.get_time()
method runs in the main thread (as it should). You should not have any long running code there. However, the loop with the time.sleep(2)
in it is long running code. As it stands now, your GUI locks up because control it not returned to the GUI event loop for 2*countto
seconds. Just remove the whole while loop. I don't really know why it is there.
Delete this:
q = 0
while q < 5:
print(countto+2)
time.sleep(2)
q += 1
The next issue comes because you recreate the QThread
each time you click the button. As such, for each new object, you need to connect the updateProgress
signal in that object to the setProgress
slot. So change the code there to:
self.wt = WorkerThread(countto)
self.wt.updateProgress.connect(self.setProgress)
self.wt.start()
At this point you will see the progress bar update correctly. However, the maximum value of the progress bar is set to 100 by default. So you might want to set the maximum value to countto
just before you create the thread. For example:
self.progressBar.setMaximum(countto)
self.wt = WorkerThread(countto)
Hope that explains things!