I would like to add a throbber to my GUI when some actions are launched.
Here is my script :
class StartTask(QtCore.QThread):
taskStarted = pyqtSignal()
def run(self):
self.taskStarted.emit()
class StopTask(QtCore.QThread):
taskStopped = pyqtSignal()
def run(self):
self.taskStopped.emit()
class Projet(object):
def __init__(self):
self.movie = '' # throbber
self.startTask = StartTask()
self.startTask.taskStarted.connect(self.startThrobber)
self.stopTask = StopTask()
self.stopTask.taskStopped.connect(self.stopThrobber)
def startThrobber(self):
# set up the movie screen on a label
self.movie_screen = QLabel()
# expand and center the label
main_layout = QVBoxLayout()
main_layout.addWidget(self.movie_screen)
ui.throbberTab2.setLayout(main_layout)
# use an animated gif file you have in the working folder
byteF = QByteArray()
movie = QMovie("D:\Various\Images\loader.gif", byteF)
movie.setCacheMode(QMovie.CacheAll)
movie.setSpeed(100)
self.movie_screen.setMovie(movie)
movie.start()
return movie
def stopThrobber(self):
movie1 = self.startThrobber()
movie1.stop()
def goAction(self):
if ui.chkbox.isChecked():
self.startTask.taskStarted.connect(self.startThrobber)
os.system(r'..........') # script launched
self.stopTask.taskStopped.connect(self.stopThrobber)
QMessageBox.information(self.popup(), "Information", "It works!")
Since it's the first time I'm using a Thread, I can't find what's wrong and where it is wrong..
This gives no result, even though i think i'm not too far away from the correct code.
I've managed to make the throbber appear but not at the correct moment (the thread was not working then).
Instead of using a
QThread
, I usedsubprocess
which works fine. I don't know the difference though, like if one is "better" than the other, or if they have small differences etc. but it does the job !