self.connect(self.mythread, QtCore.SIGNAL("mysignal(QString)"), self.on_change, QtCore.Qt.QueuedConnection)
3-ий параметр - ссылка на метод
4-ый параметр - сигнал помещается в очередь событий и обработчик должен выполняться в потоке приемника сигнала, т.е. в GUI-потоке
-----------
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
class MyThread(QtCore.QThread):
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def run(self):
for i in range(1, 21):
self.sleep(3)
# give data from thread through signal
self.emit(QtCore.SIGNAL("mysignal(QString)"), "i = %s" %i)
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.label = QtGui.QLabel("Press button for execute thread")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.button = QtGui.QPushButton("Execute process")
self.vbox = QtGui.QVBoxLayout()
self.vbox.addWidget(self.label)
self.vbox.addWidget(self.button)
self.setLayout(self.vbox)
self.mythread = MyThread() # create instance of thread
self.connect(self.button, QtCore.SIGNAL("clicked()"), self.on_clicked)
self.connect(self.mythread, QtCore.SIGNAL("started()"), self.on_started)
self.connect(self.mythread, QtCore.SIGNAL("finished()"), self.on_finished)
self.connect(self.mythread, QtCore.SIGNAL("mysignal(QString)"), self.on_change, QtCore.Qt.QueuedConnection)
def on_clicked(self):
self.button.setDisabled(True)
self.mythread.start()
def on_started(self):
self.label.setText("Call method on_started()")
def on_finished(self):
self.label.setText("Called method on_finished()")
self.button.setDisabled(False)
def on_change(self, s):
self.label.setText(s)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.setWindowTitle("Using class QThread")
window.resize(300, 70)
window.show()
sys.exit(app.exec_())