在 signal/slots 中使用自定义对象(PyQt_PyObject 类似)

Using custom object (PyQt_PyObject like) in signal/slots

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

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

TypeError: Unknown type used to call meta function (that may be a signal): PyQt_PyObject

我尝试使用 object 而不是 PyQt_PyObject,但只有在信号和插槽之间使用 DirectConnection 类型时才会发生这种情况:

self.connect(dummyEmitter,
             QtCore.SIGNAL("logMsgPlain(object)"),
             self._logMsgPlain,
             QtCore.Qt.DirectConnection)

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

QObject::connect: Cannot queue arguments of type 'object'
(Make sure 'object' is registered using qRegisterMetaType().)

我说 "things happen" 是因为它目前还行不通。由于 DirectConnection 类型,我现在收到错误:

QObject::startTimer: timers cannot be started from another thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
etc ...

我该怎么办? 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_()

目前,我找到的唯一解决方案是切换到新样式 signal/slot 语法:

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_()

但我很想知道是否有使用旧式语法的解决方案,但目前看来还没有。