PDA

View Full Version : accesing QTextEdit from thread



msmihai
8th December 2008, 12:05
Why everytime I try to call TextEdit->insertPlainText(...) from inside a thread it gives me runtime erorr? It gives this only in debug mode... release works fine.

tinsuke
8th December 2008, 12:40
You shouldn't be calling GUI methods from outside the GUI thread, doing this is totally bug-prone.

One approach to solve your problem is to use Qt signals/slots mechanism. When emitting a signal to a object from another thread, the default behaviour is to enqueue the slot execution at the receiver thread event queue.

In short, your thread (or whichever object from another thread that is doing the call) could emit a signal , i.e. addToTextEdit(const QString&), that is connected to the QTextEdit::insertPlainText(const QString&) slot. This way, the QTextEdit text will only be changed on the QTextEdit owner thread (probably the QApplication one).