python - 如何将参数传递给 QThread Worker 类?

标签 python mysql pyqt pyqt4 qthread

<分区>

我有一个工作代码示例,它创建了一个必须从我的类 (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

最佳答案

不,我认为这不是重复的问题,它还有更多的事情要做......

无论如何,你的问题你想传递更多的参数,在 python 中你可以传递许多参数调用 'yourMethod(*args, **kw)';例子;

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)
        .
        .

问候,

关于python - 如何将参数传递给 QThread Worker 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25413743/

相关文章:

python - multiprocessing.Pool in jupyter notebook 适用于 linux 但不适用于 windows

python - 如何勾选表格和模式窗口中的复选框?

python - 将 PyQt3D 窗口集成到 QMainWindow

python - PyQt 导入错误

python - 是否可以通过 y_pred 的额外预处理来制作自定义损失函数?

Python,使用列表,找到最大序列长度

php - 如何在MySQL过程中返回最后插入的id

mysql - 正向工程师未从 MySQL Workbench 中的模型创建表

html - 根据组合框选择查询数据库

qt - 在 PyQt 中模拟鼠标点击事件