Advertisement
mathomastech

Multi-threading in PyQt 4

May 21st, 2014
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. # This is an example of how to properly do multi-threading in PyQt 4.
  2. # This is a stripped down version of the code, in order to emphasize
  3. # the multi-threading proccess. To view the full code, go to
  4. # www.github.com/mathomastech/HBS_Communicator
  5. # The files of interest in handler.py and worker.py
  6.  
  7. #worker.py
  8. from PyQt4 import QtCore
  9. from communicator import Communicator
  10.  
  11. class Worker(QtCore.QThread):
  12.     update_online_list = QtCore.pyqtSignal()
  13.  
  14.     def __init__(self, parent=None):
  15.         QtCore.QThread.__init__(self)
  16.  
  17.     def run (self):
  18.         # Do a bunch of long running logic here
  19.     # Emit signal to main thread
  20.     self.update_online_list.emit()
  21.         return
  22.  
  23. # handler.py
  24. from PyQt4 import QtGui, QtCore
  25. from communicator import Communicator
  26. from worker import Worker
  27.  
  28. class Handler(QtGui.QMainWindow):
  29.    
  30.     def __init__(self):
  31.         super(Handler,self).__init__()
  32.        
  33.         # Threading
  34.         QtCore.QThread.currentThread().setObjectName("MAIN")
  35.         self.thread = QtCore.QThread()
  36.         self.thread.name = "auto_refresh"
  37.    
  38.         self.worker = Worker()
  39.         self.worker.moveToThread(self.thread)
  40.         self.worker.start()
  41.         self.worker.update_online_list.connect(self.online_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement