Is there any way we can pass a parameter to QThread when the thread is started (.start) ?
I found an example of using pyqt thread in stackoverflow, but I was wondering how to pass a parameter, in case I want the worker thread to process a data that I pass to its run() function.
The post I refer to: Busy indication with PyQt progress bar
The code:
class MyCustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyCustomWidget, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
self.progressBar = QtGui.QProgressBar(self)
self.progressBar.setRange(0,100)
button = QtGui.QPushButton("Start", self)
layout.addWidget(self.progressBar)
layout.addWidget(button)
button.clicked.connect(self.onStart)
self.myLongTask = TaskThread()
self.myLongTask.notifyProgress.connect(self.onProgress)
def onStart(self):
self.myLongTask.start()
def onProgress(self, i):
self.progressBar.setValue(i)
class TaskThread(QtCore.QThread):
notifyProgress = QtCore.pyqtSignal(int)
def run(self):
for i in range(101):
self.notifyProgress.emit(i)
time.sleep(0.1)
I want to pass a variable when calling .start such as
self.myLongTask.start(myvar)
.
.
def run(self, myvar):
but of course, pyqt does not allow that.