[PyKDE] QThread, signals and slots

Ole Morten Grodås grodaas at gmail.com
Wed Jan 10 20:32:45 GMT 2007


Hi,

I have a problem with passing signals a cross threads.
I was under the impression that when using Qt.QueuedConnection the slot
would always be executed in the thread that the receiver object lived.  But
when I created this test application I does not seem to be doing that. Any
suggestions on what the error might be?

Btw, I'm working on a tutorial on using signal and slots with python over at
http://developernew.kde.org/Development/Tutorials/101_Python_introduction_to_signal_and_slots
Feel free to improve it. Feedback and suggestions are also most welcome.
English is not my native language so I guess there are plenty of mistakes :/

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 newItem(self,item):
        self.list.addItem(item)


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

    def run(self):
        self.connect(self,SIGNAL("createdItem"),self.ui.newItem,
Qt.QueuedConnection)
        for i in range (1,10):
            #creating items
            self.emit(SIGNAL("createdItem"),i)
        self.exec_()

if __name__=="__main__":
    app=QApplication(sys.argv)
    window=MyUI()
    window.show()
    worker=MyWorkerThread(window)
    worker.start()
    app.exec_()


This code outputs:
QObject: Cannot create children for a parent that is in a different thread.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070110/dd9fa3c6/attachment.html


More information about the PyQt mailing list