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] Moving a worker object to a thread
QtWS25 Last Chance

[Solved] Moving a worker object to a thread

Scheduled Pinned Locked Moved General and Desktop
qthreadmovetothreadevent loop
11 Posts 2 Posters 6.2k 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.
  • K Offline
    K Offline
    koahnig
    wrote on 3 Jul 2015, 16:54 last edited by koahnig 7 Mar 2015, 19:40
    #1

    Basically I am follwing the first example in QThread. My object sends a signal when finished. This signal I have connected to quit() method of the QThread object.
    To my understanding this should stops the thread's event loop which is hosted by run.
    However, when checking if the thread object is still running when checking isRunning(). Is this behaviour correct?

    This is on Win7 64 bit with Qt 5.4,1 MinGW compilation.

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 3 Jul 2015, 17:16 last edited by
      #2

      Hi,

      could you show your code?
      I think is a better Idea (like in the example) to call the quit() slot from the COntroller and not from the Worker.

      If you really heve/want to do it I suggest to use Qt::QueuedConnection as ConnectionType in your connect()

      connect(worker, &Worker::done, &workerThread, &QThread::quit, Qt::QueuedConnection);
      

      in that way the quit() slot will be executed in a new eventLoop processing

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      K 1 Reply Last reply 3 Jul 2015, 18:15
      1
      • M mcosta
        3 Jul 2015, 17:16

        Hi,

        could you show your code?
        I think is a better Idea (like in the example) to call the quit() slot from the COntroller and not from the Worker.

        If you really heve/want to do it I suggest to use Qt::QueuedConnection as ConnectionType in your connect()

        connect(worker, &Worker::done, &workerThread, &QThread::quit, Qt::QueuedConnection);
        

        in that way the quit() slot will be executed in a new eventLoop processing

        K Offline
        K Offline
        koahnig
        wrote on 3 Jul 2015, 18:15 last edited by koahnig 7 May 2015, 09:40
        #3

        @mcosta
        Thanks for reply.

                MyStuff *myStuff = new MyStuff ( currentEpo, WorkingDir );
                myStuff->moveToThread ( &MyDownLoadThread );
                connect(&MyDownLoadThread, &QThread::finished, myStuff, &QObject::deleteLater);
                connect(this, &OrbitHandling::smytartMyDownLoad, myStuff, &MyStuff::sltStart );
                bool boo = connect ( myStuff, SIGNAL ( sigFinished() ), &MyDownLoadThread, SLOT ( quit() ) );
                assert ( boo );
                MyDownLoadThread.start();
                emit startMyDownLoad();
        

        MyDownLoadThread is a QThread object and MyStuffthe worker class.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 3 Jul 2015, 18:29 last edited by
          #4

          Hi,

          in that code, where you check if MyDownloadThread is still running?
          How do you know when the thread has finished??

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          K 1 Reply Last reply 3 Jul 2015, 18:37
          0
          • M mcosta
            3 Jul 2015, 18:29

            Hi,

            in that code, where you check if MyDownloadThread is still running?
            How do you know when the thread has finished??

            K Offline
            K Offline
            koahnig
            wrote on 3 Jul 2015, 18:37 last edited by koahnig 7 May 2015, 09:40
            #5

            @mcosta
            That is not in this part. I have a timer loop running. There I am checking isRunning().

                qDebug() << "MyDownloadThread is " << MyDownLoadThread.isRunning();
                if ( MyDownLoadThread.isRunning() ) 
                {
                // restart download hours later 
                }
            

            The download thread is basically downloading some files and writes them to disk. So, I can simply look at the disk for checking. Also I have set a breakpoint where the signal sigFinished() is emitted. Actually I had to set the break point, where another signal is emitted but this one is connected to sigFinished(). The thread stopped in the debugger and therefore, I think the signal is emitted.

            The actual download is pretty fast most of the time.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on 3 Jul 2015, 19:05 last edited by
              #6

              @mcosta said:

              MyDownloadThread

              is this object a QThread instance or a subclass??
              Have you tried to use Qt::QueuedConnection in connect() ??

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              K 1 Reply Last reply 3 Jul 2015, 19:15
              0
              • M mcosta
                3 Jul 2015, 19:05

                @mcosta said:

                MyDownloadThread

                is this object a QThread instance or a subclass??
                Have you tried to use Qt::QueuedConnection in connect() ??

                K Offline
                K Offline
                koahnig
                wrote on 3 Jul 2015, 19:15 last edited by
                #7

                @mcosta said:

                Qt::QueuedConnection

                It is a QThread instance.

                @mcosta said:

                Have you tried to use Qt::QueuedConnection in connect() ??

                Just did it is the same.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on 3 Jul 2015, 19:40 last edited by
                  #8

                  I found the issue. It had nothing to do with QThread.
                  I had a problem in the logic. The original signal is sent twice. I have connected in worker class the signal of another instance to an internal slot, which cleans up already. Subsequently, I did the connection of the same signal to signal of the worker.

                  Exchanging the connects did the trick. The internal clean-up did already kill the original signal transmitting instance. Therefore, the signal was never handed to outside.
                  Now, the signal is first handed out and afterwards the internal slot is called. Need to check now if there could be a new problem.

                  Anyway thanks for your help.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mcosta
                    wrote on 3 Jul 2015, 19:52 last edited by
                    #9

                    You're welcome,

                    BTW, I suggest to not call quit() from the Worker; in the future you may use the same thread with several Workers. From design point of view I prefer stop the thread from the object that create it

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    K 1 Reply Last reply 3 Jul 2015, 22:11
                    0
                    • M mcosta
                      3 Jul 2015, 19:52

                      You're welcome,

                      BTW, I suggest to not call quit() from the Worker; in the future you may use the same thread with several Workers. From design point of view I prefer stop the thread from the object that create it

                      K Offline
                      K Offline
                      koahnig
                      wrote on 3 Jul 2015, 22:11 last edited by
                      #10

                      @mcosta
                      You seem to answer my next question. You basically can shift another worker object to the thread while the first might still run.
                      Is that correct?

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mcosta
                        wrote on 3 Jul 2015, 22:15 last edited by
                        #11

                        Yep,

                        you can move as many objects you want in a single thread.

                        Once your problem is solved don't forget to:

                        • Mark the thread as SOLVED using the Topic Tool menu
                        • Vote up the answer(s) that helped you to solve the issue

                        You can embed images using (http://imgur.com/) or (http://postimage.org/)

                        1 Reply Last reply
                        0

                        6/11

                        3 Jul 2015, 19:05

                        • Login

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