[Interest] Emitting a signal from a different thread than the object lives in

Sean Harmer sean.harmer at kdab.com
Fri Aug 26 10:39:17 CEST 2016



On 26/08/2016 09:33, Etienne Sandré-Chardonnal wrote:
> Hi,
>
> I have an object which subclasses QThread for performing an asynchronous
> operation. Before any objection rises about that, I know that for most
> uses QThread subclassing is not the way to go, and creating an object
> which is pushed to a regular QThread is better, and I'm doing it 99% of
> the time, but here it was more straightforward to simply subclass
> QThread and overload run(), and no event loop is needed. This works fine.
>
> Now I would like the run() function to make the QThread subclass emit
> signals. If I just do "emit mySignal()" inside run(), my guess is this
> will not work unless there is a special mechanism handling this.
>
> Can I do this, or do I need a full event loop, an object living inside
> the QThread, and a queued connection between signals? (ie the regular
> QThread use)

Yes you can do this. Emitting signals from a QThread is fine. It is the 
thread affinity of the receiver compared to the thread from which the 
signal is emitted that is checked. Where people trip up is using a 
QThread subclass as the receiver, as the QThread object itself has 
affinity with the thread in which it was created (often the main thread).

Oh and there is also nothing wrong with subclassing QThread. Which 
approach you choose is dependent upon what you want to do.

Cheers,

Sean

>
> Thanks!
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>

-- 
Dr Sean Harmer | sean.harmer at kdab.com | Managing Director UK
KDAB (UK) Ltd, a KDAB Group company
Tel. +44 (0)1625 809908; Sweden (HQ) +46-563-540090
Mobile: +44 (0)7545 140604
KDAB - Qt Experts



More information about the Interest mailing list