[PyKDE] Signal and Slot Design problem.

Ole Morten Grodås grodaas at gmail.com
Mon Jan 8 22:52:19 GMT 2007


Hi,

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:

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


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?

An example of how I deal with the problem now:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class MyUI(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.list=QListWidget()
        self.setCentralWidget(self.list)

    def newString(self,item):
        self.list.addItem(str(item))


class MyWorkerThread(QThread):
    def __init__(self,parrent):
        QThread.__init__(self)
        self.p=parrent

    def run(self):
        list =[]
        for i in range (1,10):
            s=MySubWorkerThread(i)
            QObject.connect(s,SIGNAL("newString(PyQt_PyObject)"),
self.newString)
            s.start()
            list.append(s)
        self.exec_()

    def newString(self,host):
        self.emit(SIGNAL("newString(PyQt_PyObject)"),host)


class MySubWorkerThread(QThread):
    def __init__(self,i):
        QThread.__init__(self)
        self.i=i

    def run(self):
        self.emit(SIGNAL("newString(PyQt_PyObject)"),str(self.i))
        self.exec_()

if __name__=="__main__":
    app=QApplication(sys.argv)
    window=MyUI()
    window.show()
    worker=MyWorkerThread(window)
    worker.start()
    QObject.connect(worker,SIGNAL("newString(PyQt_PyObject)"),
window.newString)
    app.exec_()



Regards,
Ole
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070109/876db0bb/attachment.html


More information about the PyQt mailing list