我有一个代码的工作示例,它创建了一个必须从我的类(MyClass)调用的QThread。我尝试通过Worker init 传递其他参数,但我无法使其工作。
如何使用此工作代码将1个或多个参数传递给我的工作线程?
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import *
class Worker(QThread):
processdone = QtCore.pyqtSignal("QString") # Define custom signal.
def __init__(self, parent=None):
QThread.__init__(self, parent)
def run(self):
print("do worker thread processing here")
self.emit( SIGNAL('processdone'), "DONE")
return
class MyClass(QObject):
def __init__(self):
super(MyClass, self).__init__()
thread1 = Worker(self)
self.connect( thread1, SIGNAL("processdone"), self.thread1done)
thread1.start()
def thread1done(self, text):
print(text) # Print the text from the signal.
sys.exit()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
a = MyClass()
sys.exit(app.exec_())
我发现这个stackoverflow问题非常相似,但我无法得到接受的答案来使用上面的代码:Passing an argument when starting new QThread() in PyQt
答案 0 :(得分:3)
不,我认为不是重复的问题,它还有更多的事情要做......
无论如何,你的问题想要传递更多的论据,在python中你可以传递许多参数调用' yourMethod(* args,** kw)&#39 ;;例子;
class Worker(QThread):
.
.
def __init__(self, parent, *args, **kw):
QThread.__init__(self, parent)
self.yourInit(*args, **kw)
.
.
def yourInit (self, x, y, z):
print x, y, z
.
.
class MyClass(QObject):
.
.
def __init__(self):
super(MyClass, self).__init__()
.
.
x = 1000
y = 'STRING'
z = [0, 1, 2, 3, 4]
thread1 = Worker(self, x, y, z)
.
.
此致