[Qt-jambi-interest] Qt Jambi threading once again.

Gunnar Sletta gunnar at trolltech.com
Mon Mar 3 16:02:37 CET 2008


Tomasz 'Trog' Welman wrote:
> Hello.
> I have the following (common) problem.
> I have my QLabel class object created in the main thread
> (class TestWidget) an a ThreadClass class representing another
> thread. I've read this list, and Trolltech's docs and using
> this knowledge i'd moved the QLabel class object to the
> ThreadClass thread with moveToThread, but I am still having
> the same exception, which is:
> 
> Exception in thread "Thread-2" QObject used from outside its own thread, object=com.trolltech.qt.gui.QLabel at 2e7820, objectThread=Thread[main,5,main], currentThread=Thread[Thread-2,5,main]
>       	at com.trolltech.qt.QtJambiInternal.threadCheck(QtJambiInternal.java:509)
>       	at com.trolltech.qt.core.QObject.moveToThread(QObject.java:99)
>       	at test.ThreadClass.run(ThreadClass.java:19)
> 
> What am I doing wrong?

Hi Tomasz,

The main problem here is that you are trying to use QLabel from any 
other thread than the main thread. This is simply not allowed. All GUI 
objects, widgets, fonts etc can only be used from the GUI thread.

The appropriate way of updating a QLabel from a thread is to either use 
a queued connection or QApplication.invokeLater().

On a sidenote, QObject.moveToThread() can be used to move objects from 
the current thread to another thread. It can not be used to move objects 
from another thread to this thread.

> In Qt (C++) this solution works fine.

In Qt / C++ we don't have any mechanisms to prevent you from doing this, 
but it is very wrong and will crash sooner or later.

best regards,
Gunnar


More information about the Qt-jambi-interest mailing list