[PyQt] Problem regarding setting text of QTextEdit in a separate thread

Hazen Babcock hbabcock at mac.com
Thu Jul 9 00:07:04 BST 2009


Shine Jose wrote:
> Hello friends,
> I am writing an application to read data from serial port and display it 
> on the screen using a QTextEdit widget.  Since I'll have to poll the 
> serial port to check for arrival of data, I have created the widgets in 
> the main thread of the program and created a separate thread for reading 
> data. However when I try to update text of TextEdit using its setText() 
> method in the new thread, I get the following error:
> QObject: Cannot create children for parent that is in different thread
> 
> Searching on the net made me realize that setting the text in a separate 
> thread is not supported in pyQT. Is there any work-around for this 
> problem? I request you to suggest me any alternative method to find a 
> solution for my problem. 

One way is to have the thread generate a message signal that contains 
the new text & then connect the signal to the main program which handles 
updating the TextEdit.

The thread would have something like this in its run loop:
self.emit(QtCore.SIGNAL("newText(PyQt_PyObject)"), new_text)

And the main program would have something like:
self.connect(self.thread, QtCore.SIGNAL("newText(PyQt_PyObject)"), 
self.handleNewText)

This is something that you might also be able to do with timers, which 
are little simpler to use.

-Hazen



More information about the PyQt mailing list