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. How to add a C++ object into a Qt Thread ?
Forum Updated to NodeBB v4.3 + New Features

How to add a C++ object into a Qt Thread ?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 6 Posters 6.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.
  • S Offline
    S Offline
    sparshturaga
    wrote on last edited by
    #1

    Hi all,
    I have tried reading different discussions about Qt Threads and the ways to implement them which left me completely confused.
    In my case, I have a class StageController which contains all the parameters and methods for controlling a stage. I want to instantiate this class and keep the object into a new thread which runs parallel to the GUI thread. In this way, I can call the object's function whenever I want in the GUI and it implements the function on the parallel thread. But it doesn't seem to work.

    There are apparently two approaches. One would be to subclass QThread and reimplement the run function. But I want to put a whole object and not just one function.
    The other approach(more object-oriented one) is to subclass QObject in your class definition and use the moveToThread() command.
    @
    QThread StageThread;
    stage = new StageController();
    stage->moveToThread(&StageThread);
    StageThread.start();@

    In this way, I get the error saying "QThread:Destroyed while thread is still running". If I use

    @StageThread.wait();@

    as suggested in some forums, then the program hangs.
    The common example of producer and worker threads doesn't look so appealing since I would then need to use Slots and Signals for every method that my class has.

    Can somebody shed some light in this regard ? Thanks in advance :).

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Lykurg
      wrote on last edited by
      #2

      Don't know where the code is inside the application but most probably you have to create the thread on the heap!

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

        I use the code described in "this post":qt-project.org/forums/viewthread/15626/ .

        You can do this:
        @
        QThread *thread = new QThread();
        stage = new StageController();
        stage->moveToThread(thread);
        connect(stage, SIGNAL(destroyed()), thread, SLOT(quit()));
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();
        @

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #4

          [quote author="Luca" date="1333435472"]I use the code described in "this post":qt-project.org/forums/viewthread/15626/ .

          You can do this:
          @
          QThread *thread = new QThread();
          stage = new StageController();
          stage->moveToThread(thread);
          connect(stage, SIGNAL(destroyed()), thread, SLOT(quit()));
          connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
          thread->start();
          @
          [/quote]

          Now you only need to remember to delete "stage" object when you want...

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sparshturaga
            wrote on last edited by
            #5

            Thanks Lykurg and Luca for your replies and detailed answer. Creating the thread on the heap helped and I did not get that error.
            Of course, now I have to ensure that I kill the stage before killing the thread.

            Thanks guys!!

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

              You don't have to delete the thread if you set the connect statements as suggested above. Only delete your stage instance when you're done, the rest will be handled via the connected signals and slots.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sparshturaga
                wrote on last edited by
                #7

                Yes you are right KA510.. In my code, I hadn't connected the signals and slots earlier as above. So I had to quit the thread after killing the object instance manually. But Luca's connections make life easier definitely :).

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  Be aware that the first article is obsolete as of Qt 4.4, as QThread::run() is in fact no longer pure virtual and the default implementation calls QThread::exec() (as suggested in the article).

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Note that none of the articles say that you should not subclass QThread. There are still valid reasons to do so in some cases. You just have to understand what it means if you do so, and realize that for a lot of cases, it makes more sense to move a QObject-derived worker object to a vanilla QThread.

                    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