【问题标题】:PySide: emit() signal with a list as a parameterPySide:以列表为参数的 emit() 信号
【发布时间】:2014-09-15 17:04:00
【问题描述】:

我是 Python 中 GUI 应用程序开发的新手。我正在使用 PySide 开发 GUI。我需要跨两个线程传递参数的帮助。我知道如何使用自定义 Signals 和 Slot 机制。

我希望将list 从我的second thread 传输到我的main thread

Python 伪代码(我希望将列表correction_values 从我的second thread 发送到main thread):

---main thread----
self.connect(self.Tests_Start, SIGNAL("Test1_Passed()"), self.StartThread_Test1_Passed, Qt.DirectConnection)
def StartThread_Test1_Passed(self, values):
   for value in values:
      self.textEdit1.insertPlainText(value)
      self.textEdit1.insertPlainText(',')

-

---second thread----
def Tests()
   self.emit(SIGNAL("Test1_Passed()"), correction_values) # Is this way possible?

【问题讨论】:

标签: python python-2.7 signals pyside emit


【解决方案1】:

您可以使用新型的发射和信号。这比旧式容易。您只需创建信号对象;

class QCustomWidget (QtCore.QWidget):

    # create a new signal name 'Test1_Passed' and argument 'object' (Anything)
    Test1_Passed = QtCore.Signal(object)

    def __init__ (self):
        .
        .

接下来,使用'connect'连接信号;

self.Test1_Passed.connect(self.StartThread_Test1_Passed)

检查你的函数是传递变量;

@QtCore.Slot(object)
def StartThread_Test1_Passed (self, values):
    .
    .

最后,使用 'emit' 信号;

correction_values = ['1', '2', '3'] # List data-type
self.Test1_Passed.emit(correction_values)

您也可以阅读此document 以了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    相关资源
    最近更新 更多