Solved [SOLVED]What can I get using qApp->thread()
-
Hi,
I am a newbie learning programming in Qt.
Not sure if my understanding is right: the qApp->thread() returns the main thread?
I encountered this sentence as below,
object->moveToThread(qApp->thread());
does this mean to move object to main thread? If not, what does this command do?
Thank you!
-
Hi @Felicity ,
Here is clear explanation .
http://stackoverflow.com/questions/32046821/movetothreadqapp-thread-and-signal-slot -
Hi @Ni.Sumi ,
Actually I googled that post before asking this question; and no correct answer was chosen in that post.
I know what is Qt::ConnectionType, and understand how to move an object to a different thread other than the main thread, but have no idea what is qApp->thread().Could you please explain something about qApp->thread() ?
-
Hello,
qApp->thread()
expands toQCoreApplication::instance()->thread()
(qApp
is a shorthand macro). While usuallyQCoreApplication
(and its descendants) are created inmain()
and this is the recommended way of doing things, this may not always be the case. Now, when you create the application object, it creates a "fake" thread. By "fake" I mean that it creates aQThread
object that is representative of the thread in which the application object was created, but it's not started/run as a new thread. It's used for "bookkeeping" of theQObject
hierarchies.QObject
instances "live" in a thread (also referred to as "thread affinity" in the docs), and it's relevant whatQObject
belongs to what thread when having signal-slot invocations.So, the answer to your question is: Yes! It is the main thread.
As for this:
object->moveToThread(qApp->thread());
You can see that code in a thread different from the main/gui thread. It pushes the object back to the main thread (objects can only be "pushed" into a thread, but can't be "pulled" from another).
Kind regards.
-
@kshegunov
Thank you so much!You explained everything!