Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    4
    6
    7049
    Loading More Posts
    • 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
      ronM71 last edited by

      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 Reply Quote 0
      • R
        ronM71 last edited by

        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 Reply Quote 0
        • L
          LinusA last edited by

          [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 Reply Quote 0
          • R
            ronM71 last edited by

            Thanks for the tip. I'll adopt.

            1 Reply Last reply Reply Quote 0
            • A
              astodolski last edited by

              [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 Reply Quote 0
              • K
                KA51O last edited by

                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 Reply Quote 0
                • First post
                  Last post