Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]What can I get using qApp->thread()
Forum Update on Monday, May 27th 2025

[SOLVED]What can I get using qApp->thread()

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Felicity
    wrote on last edited by Felicity
    #1

    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!

    1 Reply Last reply
    0
    • Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by
      #2

      Hi @Felicity ,

      Here is clear explanation .
      http://stackoverflow.com/questions/32046821/movetothreadqapp-thread-and-signal-slot

      F 1 Reply Last reply
      1
      • Ni.SumiN Ni.Sumi

        Hi @Felicity ,

        Here is clear explanation .
        http://stackoverflow.com/questions/32046821/movetothreadqapp-thread-and-signal-slot

        F Offline
        F Offline
        Felicity
        wrote on last edited by
        #3

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

        kshegunovK 1 Reply Last reply
        0
        • F Felicity

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @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.

          Read and abide by the Qt Code of Conduct

          F 1 Reply Last reply
          3
          • kshegunovK kshegunov

            @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.

            F Offline
            F Offline
            Felicity
            wrote on last edited by
            #5

            @kshegunov
            Thank you so much!

            You explained everything!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved