Hi,<br><br>I have some problems with the signal and slots mechanism in threaded applications. The problem appears when the QObject.connect() call have to be made in another thread then the receiving slot. One example is if I have a worker thread that dynamically creates other worker threads. When I make the connect() call in the worker thread and connect to a slot that for example adds items to a QWidgetList I get the following error:
<br><br>QObject::connect: Cannot queue arguments of type &#39;QModelIndex&#39;<br>
(Make sure &#39;QModelIndex&#39; is registed using qRegisterMetaType().)<br><br><br>I found a way to handle the problem, but It is a rather ugly hack. I was therefore wondering if there was a better way?<br><br>An example of how I deal with the problem now:
<br><br>from PyQt4.QtGui import *<br>from PyQt4.QtCore import *<br>import sys<br><br>class MyUI(QMainWindow):<br>&nbsp;&nbsp;&nbsp; def __init__(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QMainWindow.__init__(self)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.list=QListWidget()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.setCentralWidget(self.list)<br>&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; def newString(self,item):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.list.addItem(str(item))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>class MyWorkerThread(QThread):<br>&nbsp;&nbsp;&nbsp; def __init__(self,parrent):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QThread.__init__(self)
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.p=parrent<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; def run(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list =[] <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in range (1,10):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s=MySubWorkerThread(i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QObject.connect(s,SIGNAL(&quot;newString(PyQt_PyObject)&quot;),
self.newString)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.start()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list.append(s)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.exec_()&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; def newString(self,host):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.emit(SIGNAL(&quot;newString(PyQt_PyObject)&quot;),host)<br><br>
<br>class MySubWorkerThread(QThread):<br>&nbsp;&nbsp;&nbsp; def __init__(self,i):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QThread.__init__(self)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.i=i<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; def run(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.emit(SIGNAL(&quot;newString(PyQt_PyObject)&quot;),str(
self.i))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.exec_()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>if __name__==&quot;__main__&quot;:<br>&nbsp;&nbsp;&nbsp; app=QApplication(sys.argv)<br>&nbsp;&nbsp;&nbsp; window=MyUI()<br>&nbsp;&nbsp;&nbsp; window.show()<br>&nbsp;&nbsp;&nbsp; worker=MyWorkerThread(window)<br>&nbsp;&nbsp;&nbsp; worker.start()<br>
&nbsp;&nbsp;&nbsp; QObject.connect(worker,SIGNAL(&quot;newString(PyQt_PyObject)&quot;),window.newString)<br>&nbsp;&nbsp;&nbsp; app.exec_()<br>&nbsp;&nbsp; &nbsp;<br><br><br>Regards,<br>Ole<br>