Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import time
- from PySide import QtGui
- from PySide import QtCore
- class Worker(QtCore.QObject):
- finished = QtCore.Signal(bool)
- def __init__(self):
- super(Worker, self).__init__()
- def helloWorld(self):
- for i in xrange(10):
- print 'hello %s' % i
- time.sleep(.5)
- class MainUI(QtGui.QWidget):
- def __init__(self, parent=None):
- super(MainUI, self).__init__(parent)
- self.extraThread = QtCore.QThread()
- self.worker = Worker()
- self.worker.moveToThread(self.extraThread)
- self.setupUI()
- self.connectSignalsAndSlots()
- def setupUI(self):
- # CREAT MAIN LAYOUT AND WIDGETS
- mainLayout = QtGui.QVBoxLayout()
- btnLayout = QtGui.QHBoxLayout()
- mainLayout.addLayout(btnLayout)
- self.setLayout(mainLayout)
- self.progressBar = QtGui.QProgressBar(self)
- self.progressBar.setVisible(False)
- self.btnWork = QtGui.QPushButton('Do Work')
- self.btnCancel = QtGui.QPushButton('Cancel')
- self.btnCancel.setDisabled(True)
- self.outputWindow = QtGui.QTextEdit()
- mainLayout.addWidget(self.progressBar)
- mainLayout.addWidget(self.outputWindow)
- btnLayout.addWidget(self.btnWork)
- btnLayout.addWidget(self.btnCancel)
- def connectSignalsAndSlots(self):
- print 'connecting signals'
- self.btnWork.clicked.connect(self.startWorker)
- self.btnCancel.clicked.connect(self.extraThread.terminate)
- # THREAD STARTED
- self.extraThread.started.connect(lambda: self.btnWork.setDisabled(True))
- self.extraThread.started.connect(lambda: self.btnCancel.setEnabled(True))
- self.extraThread.started.connect(self.progressBar.show)
- self.extraThread.started.connect(self.worker.helloWorld)
- # THREAD FINISHED
- self.extraThread.finished.connect(lambda: self.btnCancel.setDisabled(True))
- self.extraThread.finished.connect(self.extraThread.deleteLater)
- self.extraThread.finished.connect(self.worker.deleteLater)
- # SHOW PROGRESS BAR WHEN PUBLISHING STARTS
- self.extraThread.started.connect(self.progressBar.show)
- # CONNECT PROCESS TO PROGRESS BAR AND OUTPUT WINDOW
- # NOT DONE YET
- def startWorker(self):
- # GO
- print 'starting worker thread'
- self.extraThread.start()
- for i in xrange(10):
- print 'from main thread:', i
- time.sleep(.3)
- if __name__ == '__main__':
- args = sys.argv
- app = QtGui.QApplication(args)
- p = MainUI()
- p.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement