Issue
I manage to use PySide instead of PyQt because of licensing.
I need to pass custom objets between threads using the signal/slots mechanism. With PyQt, I can use the PyQt_PyObject
type as signal argument but obviously, this type doesn't exists in PySide :
TypeError: Unknown type used to call meta function (that may be a signal): PyQt_PyObject
I tried to use object
instead of PyQt_PyObject
but things happen only with a DirectConnection type between signal and slot :
self.connect(dummyEmitter,
QtCore.SIGNAL("logMsgPlain(object)"),
self._logMsgPlain,
QtCore.Qt.DirectConnection)
With a QueuedConnection, I get an error :
QObject::connect: Cannot queue arguments of type 'object'
(Make sure 'object' is registered using qRegisterMetaType().)
I say "things happen" because it doesn't work so far. I now get errors due to the DirectConnection type :
QObject::startTimer: timers cannot be started from another thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
etc ...
How should I do ? Is there a PyQt_PyObject type-like in PySide ?
EDIT: This small exemple will fail :
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_()
Solution
For now, the only solution I found is to switch to new style signal/slot syntax :
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_()
But I would be interested to know if there is a solution with the old-style syntax but for now, it seems that there is not.
Answered By - Loïc G.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.