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

时间:2017-12-01 23:06:08

标签: python pyside

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

我需要使用信号/插槽机制在线程之间传递自定义对象。使用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().)

我说“事情发生了”,因为它到目前为止还不起作用。我现在因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_()

1 个答案:

答案 0 :(得分:1)

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

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

但我有兴趣知道是否有旧式语法的解决方案,但就目前而言,似乎没有。