[Qt-interest] Best practices for a thread object

Sean Harmer sean.harmer at maps-technology.com
Sun Jan 16 17:18:10 CET 2011


Hi,

On 16/01/2011 15:50, Etienne Sandré-Chardonnal wrote:
> In my application, I define a "Worker" class, which inherits QThread.
> This worker class lives its own life in its thread and does computations.
<snip>
> Is it possible or stupid to move a QThread to itself with moveToThread?
> Should I do this before or after calling run() ?

Please search the list archives. This question seems to come up every 
two weeks or so at the moment.

Recommended practise is to put your work into a class that inherits 
QObject. Then create this object in the run() function of your QThread 
(or create it in the main thread and then move it to your QThread).

> What is the exact meaning of the belonging of a QObject to a QThread? I
> do not see which mechanism is involved.

This is explained in the docs about threading with QObjects. The short 
answer is that asynchronous stuff (sockets, cross-thread signals/slots, 
events etc.) will work in that thread as you would expect if the QObject 
has affinity with that thread.

Good luck,

Sean



More information about the Qt-interest-old mailing list