Skip to content

Instantly share code, notes, and snippets.

@arkottke
Created April 14, 2016 04:03
Show Gist options
  • Save arkottke/7c1c33fc6fd697cde9c88c2b47fa06ea to your computer and use it in GitHub Desktop.
Save arkottke/7c1c33fc6fd697cde9c88c2b47fa06ea to your computer and use it in GitHub Desktop.
Example of using QThread and communicating progress
import time
from PySide import QtCore, QtGui
class Simulation(QtCore.QThread):
updateRange = QtCore.Signal(int, int)
updateProgress = QtCore.Signal(int)
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
self._count = 0
@QtCore.Slot(int)
def setCount(self, count):
self._count = count
def run(self):
self.updateRange.emit(0, self._count)
for i in range(self._count):
print(i)
self.updateProgress.emit(i)
time.sleep(10)
self.updateProgress.emit(i + 1)
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.simulation = Simulation()
layout = QtGui.QGridLayout()
self.progressBar = QtGui.QProgressBar()
self.simulation.updateRange.connect(self.progressBar.setRange)
self.simulation.updateProgress.connect(self.progressBar.setValue)
layout.addWidget(self.progressBar, 0, 0, 1, 3)
self.countSpinBox = QtGui.QSpinBox()
self.countSpinBox.setRange(0, 10)
self.countSpinBox.setValue(2)
layout.addWidget(QtGui.QLabel('Count:'), 1, 0)
layout.addWidget(self.countSpinBox, 1, 1)
self.pushButton = QtGui.QPushButton('Run')
self.pushButton.clicked.connect(self.start)
layout.addWidget(self.pushButton, 1, 2)
self.setLayout(layout)
@QtCore.Slot()
def start(self):
self.simulation.setCount(self.countSpinBox.value())
self.simulation.start()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment