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 Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.0k Views 1 Watching
  • 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 26 Apr 2016, 19:57 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
    • N Offline
      N Offline
      Ni.Sumi
      wrote on 26 Apr 2016, 20:02 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 26 Apr 2016, 20:08
      1
      • N Ni.Sumi
        26 Apr 2016, 20:02

        Hi @Felicity ,

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

        F Offline
        F Offline
        Felicity
        wrote on 26 Apr 2016, 20:08 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() ?

        K 1 Reply Last reply 26 Apr 2016, 20:17
        0
        • F Felicity
          26 Apr 2016, 20:08

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

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 26 Apr 2016, 20:17 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 26 Apr 2016, 22:54
          3
          • K kshegunov
            26 Apr 2016, 20:17

            @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 26 Apr 2016, 22:54 last edited by
            #5

            @kshegunov
            Thank you so much!

            You explained everything!

            1 Reply Last reply
            0

            5/5

            26 Apr 2016, 22:54

            • Login

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