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] Qt: Main thread signal + worker thread slot.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Qt: Main thread signal + worker thread slot.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 7.4k 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.
  • R Offline
    R Offline
    ronM71
    wrote on last edited by
    #1

    Say I wanted to have a worker thread that has slots for signals emmited from the main application thread.

    How would the "::run()" method of that worker thread look? obviously I need some kind of loop or the thread will terminate immediately. I want it to stick around so it can process the incoming slots. How would that loop look like? Something equivalent to sleep and pump messages? pump how?

    Do I need to "MoveToThread()" all the objects that'll pass to the thread's slots so that the processing of the slot is processed in the context of the worker thread?

    Thank you.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      ronM71
      wrote on last edited by
      #2

      Did some more digging.

      The default implementation of QThread::run calls "exec" which is the message loop. So no extra work needed there. In the constructor of my QThread I add this:

      @this->start();
      QObject::moveToThread(this);@

      As a result, my thread starts upon construction, and all signals outside the thread aimed at thread slots are executed in the context of my thread.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LinusA
        wrote on last edited by
        #3

        [quote author="ronM71" date="1311713565"]
        @this->start();
        QObject::moveToThread(this);@
        [/quote]
        It's generally discouraged to call
        @
        moveToThread(this);
        @

        Have a look at this blog post: http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/

        More on this topic here:
        http://blog.exys.org/entries/2010/QThread_affinity.html

        So you don't need to subclass QThread at all. You can simply use "moveToThread" to push any QObject to another thread, and the slots of that object will be executed by that thread. A nice example can be found here: http://chaos.troll.no/~bhughes/producerconsumer2.tar.gz

        1 Reply Last reply
        0
        • R Offline
          R Offline
          ronM71
          wrote on last edited by
          #4

          Thanks for the tip. I'll adopt.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            astodolski
            wrote on last edited by
            #5

            [quote author="LinusA" date="1311714232"]
            [quote author="ronM71" date="1311713565"]
            @this->start();
            QObject::moveToThread(this);@
            [/quote]
            It's generally discouraged to call
            @
            moveToThread(this);
            @

            Have a look at this blog post: http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/

            More on this topic here:
            http://blog.exys.org/entries/2010/QThread_affinity.html
            That link above is a dead end!
            [/quote]

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on last edited by
              #6

              Try these links:
              "You're doing it wrong":http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/
              "best practise for QThread":http://qt-project.org/wiki/QThreads_general_usage
              "doc note in QThread reference":http://qt-project.org/doc/qt-4.8/QThread.html#notes

              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