python - Using QThread for a throbber -


i add throbber gui when actions launched.

here 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 movie screen on label         self.movie_screen = qlabel()         # expand , center label         main_layout = qvboxlayout()         main_layout.addwidget(self.movie_screen)         ui.throbbertab2.setlayout(main_layout)         # use animated gif file have in 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 first time i'm using thread, can't find what's wrong , wrong..

this gives no result, though think i'm not far away correct code.

i've managed make throbber appear not @ correct moment (the thread not working then).

your code has problem in call os.system() (which presumably long running, hence need throbber) being executed in main thread. blocks gui (and qt event loop). qmovie requires functioning event loop animate correctly. current code, there nothing can make throbber appear animated, requires responsive gui (or more precisely responsive event loop).

as aside, not allowed directly call gui methods thread. can emit events main thread secondary thread (as doing). means cannot offload qmovie thread.

so instead, need offload call os.system() thread, , emit signal when done. signal can connected slot stops throbber. before launching thread, directly call existing startthrobber() method begin throbber.

this way, ui remains responsive (including correctly showing throbber animation) while thread deals long running process blocking up.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -