在信号/插槽中使用自定义对象(类似 PyQt_PyObject)

Loï*_* G. 2 python pyside

由于许可,我设法使用 PySide 而不是 PyQt。

我需要使用信号/插槽机制在线程之间传递自定义对象。使用 PyQt,我可以使用该PyQt_PyObject类型作为信号参数,但显然,这种类型在 PySide 中不存在:

TypeError: Unknown type used to call meta function (that may be a signal): PyQt_PyObject
Run Code Online (Sandbox Code Playgroud)

我试图使用object而不是,PyQt_PyObject但事情只发生在信号和插槽之间的 DirectConnection 类型:

self.connect(dummyEmitter,
             QtCore.SIGNAL("logMsgPlain(object)"),
             self._logMsgPlain,
             QtCore.Qt.DirectConnection)
Run Code Online (Sandbox Code Playgroud)

使用 QueuedConnection,我收到一个错误:

QObject::connect: Cannot queue arguments of type 'object'
(Make sure 'object' is registered using qRegisterMetaType().)
Run Code Online (Sandbox Code Playgroud)

我说“事情发生了”,因为它到目前为止还行不通。由于 DirectConnection 类型,我现在遇到错误:

QObject::startTimer: timers cannot be started from another thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
etc ...
Run Code Online (Sandbox Code Playgroud)

我应该怎么做 ?PySide 中是否有类似 PyQt_PyObject 的类型?

编辑: 这个小例子会失败:

from PySide import QtCore, QtGui
import sys

class Object(QtCore.QObject):
    ''' A dummy emitter that send a list to the thread '''
    def emitSignal(self):
        someList = [0, 1, 2, 3]
        self.emit(QtCore.SIGNAL("aSignal(object)"), someList)

class Worker(QtCore.QObject):
    def aSlot(self, value):
        print "List: {}".format(value)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    worker = Worker()
    obj = Object()

    thread = QtCore.QThread()
    worker.moveToThread(thread)
    QtCore.QObject.connect(obj, QtCore.SIGNAL("aSignal(object)"), worker.aSlot)
    # The exemple will pass with the line below uncommented
    # But obviously, I can't use a DirectConnection with a worker thread and the GUI thread
    # QtCore.QObject.connect(obj, QtCore.SIGNAL("aSignal(object)"), worker.aSlot, QtCore.Qt.DirectConnection)

    thread.start()
    obj.emitSignal()

    app.exec_()
Run Code Online (Sandbox Code Playgroud)

Loï*_* G. 5

目前,我找到的唯一解决方案是切换到新样式的信号/插槽语法:

from PySide import QtCore, QtGui
import sys

class Object(QtCore.QObject):
    aSignal = QtCore.Signal(object)
    def emitSignal(self):
        someList = [0, 1, 2, 3]
        self.aSignal.emit(someList)

class Worker(QtCore.QObject):
    def aSlot(self, value):
        print "List: {}".format(value)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    worker = Worker()
    obj = Object()

    thread = QtCore.QThread()
    worker.moveToThread(thread)
    obj.aSignal.connect(worker.aSlot)

    thread.start()
    obj.emitSignal()

    app.exec_()
Run Code Online (Sandbox Code Playgroud)

但我很想知道是否有旧式语法的解决方案,但目前似乎没有。

  • 解决方案是**从不** 使用旧式语法,这种语法很丑陋、非pythonic 且容易出错。 (3认同)