[Interest] Emitting signal from QThread::run()

Alejandro Exojo suy at badopi.org
Sun Feb 28 10:02:00 CET 2016


El Sunday 28 February 2016, Sze Howe Koh escribió:
> > Will be queued without the explicit QueuedConnection? In other words,
> > what does AutoConnection do here? run() is executed in another thread,
> > but the QThread is in the same thread of MainWindow, right?
> 
> See http://doc.qt.io/qt-5/qt.html#ConnectionType-enum
> 
> "If the receiver lives in the thread that emits the signal,
> Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used.
> The connection type is determined when the signal is emitted."
> 
> Qt checks to see which thread the signal is emitted from. Qt does not
> check which thread the signal sender lives in.

Thank you. I don't think that the documentation is totally clear in this 
regard. The quote you pasted has a link to the explanation of thread affinity 
which says:

"By default, a QObject lives in the thread in which it is created. An object's 
thread affinity can be queried using thread() and changed using 
moveToThread()."

In the context of the previous example, I wasn't entirely sure if this was a 
possibility:

1. The QThread-derived instance is created in the thread of MainWindow.
2. That instance is not moved to other thread, so it keeps the thread affinity 
of the MainWindow.
3. When a signal is emitted in run(), the thread affinity checked is somehow 
"stored" in the instance, and since moveToThread did not change it, it's still 
the same of MainWindow.

Now I have the confirmation that the 3rd point is wrong, which is good. :)

I think at some point (when learning about QThread, etc.) we've all checked it 
ourselves calling this->thread() and QThread::currentThread() to see what they 
returned. :)

-- 
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net



More information about the Interest mailing list