Advertisement
Guest User

Untitled

a guest
Jan 24th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import sys
  2. import time
  3. from PySide import QtGui
  4. from PySide import QtCore
  5.  
  6. class Worker(QtCore.QObject):
  7.    
  8.     finished = QtCore.Signal(bool)
  9.  
  10.     def __init__(self):
  11.         super(Worker, self).__init__()
  12.  
  13.     def helloWorld(self):
  14.         for i in xrange(10):
  15.             print 'hello %s' % i
  16.             time.sleep(.5)
  17.  
  18. class MainUI(QtGui.QWidget):
  19.    
  20.     def __init__(self, parent=None):
  21.  
  22.         super(MainUI, self).__init__(parent)
  23.         self.extraThread = QtCore.QThread()
  24.         self.worker = Worker()
  25.         self.worker.moveToThread(self.extraThread)
  26.         self.setupUI()
  27.         self.connectSignalsAndSlots()
  28.  
  29.        
  30.     def setupUI(self):
  31.         # CREAT MAIN LAYOUT AND WIDGETS
  32.         mainLayout = QtGui.QVBoxLayout()
  33.         btnLayout = QtGui.QHBoxLayout()
  34.         mainLayout.addLayout(btnLayout)
  35.         self.setLayout(mainLayout)
  36.  
  37.         self.progressBar = QtGui.QProgressBar(self)
  38.         self.progressBar.setVisible(False)
  39.         self.btnWork = QtGui.QPushButton('Do Work')
  40.         self.btnCancel = QtGui.QPushButton('Cancel')
  41.         self.btnCancel.setDisabled(True)
  42.  
  43.         self.outputWindow = QtGui.QTextEdit()
  44.        
  45.         mainLayout.addWidget(self.progressBar)
  46.         mainLayout.addWidget(self.outputWindow)
  47.        
  48.         btnLayout.addWidget(self.btnWork)
  49.         btnLayout.addWidget(self.btnCancel)
  50.  
  51.  
  52.     def connectSignalsAndSlots(self):
  53.         print 'connecting signals'
  54.         self.btnWork.clicked.connect(self.startWorker)
  55.         self.btnCancel.clicked.connect(self.extraThread.terminate)
  56.  
  57.         # THREAD STARTED
  58.         self.extraThread.started.connect(lambda: self.btnWork.setDisabled(True))
  59.         self.extraThread.started.connect(lambda: self.btnCancel.setEnabled(True))
  60.         self.extraThread.started.connect(self.progressBar.show)
  61.         self.extraThread.started.connect(self.worker.helloWorld)
  62.  
  63.         # THREAD FINISHED
  64.         self.extraThread.finished.connect(lambda: self.btnCancel.setDisabled(True))
  65.         self.extraThread.finished.connect(self.extraThread.deleteLater)
  66.         self.extraThread.finished.connect(self.worker.deleteLater)
  67.        
  68.         # SHOW PROGRESS BAR WHEN PUBLISHING STARTS
  69.         self.extraThread.started.connect(self.progressBar.show)
  70.         # CONNECT PROCESS TO PROGRESS BAR AND OUTPUT WINDOW
  71.         # NOT DONE YET
  72.  
  73.     def startWorker(self):
  74.         # GO
  75.         print 'starting worker thread'
  76.         self.extraThread.start()
  77.         for i in xrange(10):
  78.             print 'from main thread:', i
  79.             time.sleep(.3)
  80.  
  81.  
  82.  
  83. if __name__ == '__main__':
  84.     args = sys.argv
  85.     app = QtGui.QApplication(args)
  86.     p = MainUI()
  87.     p.show()
  88.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement