Advertisement
Guest User

PyQt Threads

a guest
Oct 23rd, 2014
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. #!/bin/python
  2.  
  3. from PySide.QtCore import *
  4. from PySide.QtGui import *
  5. import sys
  6. import time
  7.  
  8. class PrintSignal(QObject):
  9.     printer = Signal(str)
  10.  
  11. class OtherProcess(QThread):
  12.     signal = PrintSignal()
  13.     def __init__(self):
  14.         super(OtherProcess, self).__init__()
  15.  
  16.     def run(self):
  17.         v = 0
  18.         while True:
  19.             time.sleep(1)
  20.             v += 1
  21.             self.signal.printer.emit("Hello %d from thread !" % v)
  22.             if v > 9:
  23.                 break
  24.         self.signal.printer.emit("Bye")
  25.  
  26.  
  27. class UI(QWidget):
  28.     def __init__(self):
  29.         super(UI, self).__init__()
  30.         vl = QVBoxLayout()
  31.         self.console = QTextBrowser()
  32.         vl.addWidget(self.console)
  33.         self.setLayout(vl)
  34.  
  35.         self.otherProcess = OtherProcess()
  36.         self.otherProcess.signal.printer.connect(self.console.append)
  37.         self.otherProcess.start()
  38.  
  39.     def closeEvent(self, *args, **kwargs):
  40.         # Wainitng for thread to commplete his work
  41.         self.otherProcess.wait()
  42.  
  43. app = QApplication(sys.argv)
  44. ui = UI()
  45. ui.show()
  46. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement