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

Gunnar Sletta gunnar at trolltech.com
Wed Mar 5 08:51:04 CET 2008


Tomasz 'Trog' Welman wrote:
> On Mon, 3 Mar 2008, Gunnar Sletta wrote:
> 
>> 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().
>>
> What is a queued connection? Which object should I use to obtain it?
> If I understand well, invokeLater() runs a thread, when the gui thread
> is doing nothing (is in idle state)?

Hi Thomasz,

A queued connection is an asynchronous signal slot connection. Instead 
of being a standard function call, the arguments are cached and posted 
as an event in the reciver threads event loop. When the event is 
delivered the function is called. Its a thread safe connection. By 
default, connections are "auto" which means we figure out at runtime if 
a slot should be called queued or direct.

// In GUI thread setup
workerThread.updateSignal.connect(label, "setText(String)");


// In Worker Thread
updateSignal.emit("Now updated...");


>> 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.
>>
> You are talking about Jambi or Qt also? I'm asking because I run a thread
> in C++ (QThread) form which (in run method) I'm invokeing:
> socket->moveToThread(thread());
> So i am pulling socket form the main thread to this thread.
> Is this a good solution? If not, what shoult I do?
> PS: I have no errors concerning that.

Both C++ and Qt Jambi docs clearly state that pulling is not the most 
fortunate thing to do ;-) I would reverse the logic and push the socket 
to the main thread from its created thread.

http://doc.trolltech.com/main-snapshot/qobject.html#moveToThread
http://doc.trolltech.com/qtjambi-4.3.3_01/com/trolltech/qt/core/QObject.html#moveToThread(java.lang.Thread)

best regards,
Gunnar


More information about the Qt-jambi-interest mailing list