[Qt-interest] Threading and objects...

Sean Harmer sean.harmer at maps-technology.com
Fri Nov 12 09:57:46 CET 2010


Hi,

On Thursday 11 November 2010 16:54:36 BRM wrote:
> I have been doing quite a bit of multithreading with Qt for a while; and
> recently read (on the Qt blogs, a couple months back[1]) about the "right
> way" to use QThread;
<snip>

> In my new approach, I'm trying to keep the same basic architecture, at
> least for now while I get to figure out the new approach - 
<snip>

The approach that I normally take to this is to have a trivial subclass of 
QThread that contains as members pointers to the one or more objects that 
actually do the work in the context of the thread.

There is no need to have forwarding signals from the thread class itself. Just 
expose the pointers to the worker objects, then you can simply use them to 
make connections between the worker objects and whatever else you have.

I normally create the worker objects inside the run() function of my QThread 
subclass. The only thing to be careful of is to make sure that your object is 
created before you try to use it in a connect() call. To help with this I 
usually use a simple waitForStart() function (see attached example). The 
alternative is to use moveToThread() but I find this less maintainable.

The attched example (which I posted for a similar threading question back in 
May) shows this approach.

HTH,

Sean
-------------- next part --------------
A non-text attachment was scrubbed...
Name: threadedpoller.zip
Type: application/zip
Size: 4483 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101112/bbd3628c/attachment.zip 


More information about the Qt-interest-old mailing list