Solved QLabel and QSlider stop updating after a period (using QThread)
-
I am using a thread to setpixmap on a label, and also setValue to change the location of a slider.
def set_slider(self, currentFrame, lastFrame): print(f"Setting slider to: {currentFrame}") self.slider.setRange(0,lastFrame) self.slider.setValue(currentFrame)
The code above is being called correctly every update loop in the thread and the print outs are ticking up as I would expect.
However after a period it pauses. I checked the values and everything is still updating and the function is being called correctly, however none of the changes are rendered.
It feels almost as though the UI is going into sleep mode (I am on a Mac), and I need to wake it up (but selecting the window does nothing, only clicking some kind of button "restarts" the video).
If I click a test button that does absolutely nothing at all (not hooked up to any function), the UI will begin rendering again.
I tried calling QApplication.processEvents() after updating the slider but it didn't help.
-
@Willow said in QLabel and QSlider stop updating after a period (using QThread):
def set_slider(self, currentFrame, lastFrame):
print(f"Setting slider to: {currentFrame}")
self.slider.setRange(0,lastFrame)
self.slider.setValue(currentFrame)You execute this in a thread?
If so - don't!
You should never change UI from other thread than GUI thread.
Emit a signal from the thread to update the UI. -
This message was delayed for 2400 seconds due to Troll issues on this forum
If you could supply a MRE (Minimal Reproducible Example) that can just be copied/pasted and ran that would facilitate answer this question much more easily.
Also since you do not show it are you using QThread correctly. I ask because the current documentation might still say you are to sub-class a QThread but that is woefully wrong in Qt5 but the documentation was (last time I checked) still Qt4 oriented. So while you can do it this way it does not work well this way.
Next QThreads are serial in nature due to the Python GIL -- this means if you are trying to execute 2 (or more) long or on-going processes they are going to step all over each other and one will end up blocking the their because QThread1 will run until it completes, then QThread2 will run until it completes, then etc... So this might be part of the problem but without an MRE its extremely hard to tell
-
@jsulm said in QLabel and QSlider stop updating after a period (using QThread):
@Willow said in QLabel and QSlider stop updating after a period (using QThread):
def set_slider(self, currentFrame, lastFrame):
print(f"Setting slider to: {currentFrame}")
self.slider.setRange(0,lastFrame)
self.slider.setValue(currentFrame)You execute this in a thread?
If so - don't!
You should never change UI from other thread than GUI thread.
Emit a signal from the thread to update the UI.Thanks for the help, I think this is what caused the issue, I changed it to a signal and now it doesn't freeze up anymore!
@Denni-0 thanks also for the help, however it seems to be working now. Seems like the issue was caused by manually updating from the thread instead of emitting a signal.
-
This message was delayed for 600 seconds due to Troll issues on this forum
@Willow I have to admit I am a bit concerned that you might not be implementing whatever it is you are doing in an efficient manner but without seeing what you are actually doing I cannot say that for sure. It just sounds like what you describe might be better handled in a somewhat different manner but then again perhaps what you are doing is the way to go. If you would like some feedback on it let me know otherwise happy coding.