[Qt-interest] How to do it... in a QThread?

Sean Harmer sean.harmer at maps-technology.com
Fri Aug 5 10:44:53 CEST 2011


On Friday 05 August 2011 10:38:39 Sven Anderson wrote:
> Hi,
> 
> Am 05.08.2011 00:49, schrieb K. Frank:
> > If, for example, your design is such that you derive your own thread
> > class from QThread, you could pass pABCD to that class's constructor. 
> > The run method of the QThread-derived class will now have access to A,
> > B, C, and D through its copy of pABCD struct.
> 
> just a short note, that subclassing QThread is not recommended anymore.
> Read the comment on the QThread documentation in the new documentation
> platform:
> 
> http://developer.qt.nokia.com/doc/qt-4.7/qthread.html

Subclassing QThread is still a valid approach as long as you don't abuse it. I 
find a useful pattern is to subclass QThread and make a worker QObject a 
member of the QThread subclass. I then create the worker object in the run() 
function which then becomes analogous to main() in the main thread.

If you find yourself adding signals/slots to the QThread subclass you are 
likely heading for trouble unless you really understand the Qt thread-object 
affinity model.

Cheers,

Sean




More information about the Qt-interest-old mailing list