[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 @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() ?


  • Qt Champions 2016

    @Felicity

    Hello,
    qApp->thread() expands to QCoreApplication::instance()->thread() (qApp is a shorthand macro). While usually QCoreApplication (and its descendants) are created in main() 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 a QThread 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 the QObject hierarchies. QObject instances "live" in a thread (also referred to as "thread affinity" in the docs), and it's relevant what QObject 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.