Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt thread which will run multiple times for application life time
Forum Updated to NodeBB v4.3 + New Features

Qt thread which will run multiple times for application life time

Scheduled Pinned Locked Moved Mobile and Embedded
10 Posts 5 Posters 6.8k 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
    raj.qtdev
    wrote on last edited by
    #1

    Hi,

    I want to have a non-ui thread in my app which should start when the app is launched and will survive during the lifetime of the app. The run() method of this thread should be able to run multiple times during the application lifetime.

    I tried to create a new thread subclassing QThread but the issue is that the run() method executes just one time and the thread completes. Then I do not have any way to start the thread again.

    Any suggestions on how to do this?

    Thanks

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

      Does it have to be the run function of QThread itself? Why not create a worker object, move it to your thread, add a slot to this object and trigger that slot (via a queued connection, of course) when needed?

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hardcodes.de
        wrote on last edited by
        #3

        You could give QRunnable a try, if that inherits QObject (could have been the other way around) it gets an event loop and then you can trigger the action any time as long as the object lives in your application.

        while(!sleep){++sheep;}

        1 Reply Last reply
        0
        • N Offline
          N Offline
          NetZwerg
          wrote on last edited by
          #4

          Actually from your description you should not subclass QThread but create an QObject-subclass with a given slot your work is progressed in. Then just move your object to a QThread:

          MyObject * obj;
          Qthread *MyThread = new QThread;
          obj->moveToThread(MyThread);
          Mythread->start();

          connect(this,SIGNAL(startMyProgressSLot),obj,SLOT(RunMyProgress));
          emit signalStartMyProgressSlot();

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raj.qtdev
            wrote on last edited by
            #5

            thanks guys but can somebody provide a sample code ....

            @Andre: i have overridden the run of QThread which should be called again and again to perform the task

            @hardcodes.de: yes i need a kind of event loop

            thanks

            1 Reply Last reply
            0
            • R Offline
              R Offline
              raj.qtdev
              wrote on last edited by
              #6

              thanks NetZwerg...will give that a try

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

                [quote author="raj.qtdev" date="1380544075"]thanks guys but can somebody provide a sample code ....

                @Andre: i have overridden the run of QThread which should be called again and again to perform the task

                @hardcodes.de: yes i need a kind of event loop

                thanks[/quote]

                Yeah, I know. That's what you said. And I suggested you may be better of not doing that and using a different way instead, like NetZwerg said.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  raj.qtdev
                  wrote on last edited by
                  #8

                  MyObject * obj;
                  Qthread *MyThread = new QThread;
                  obj->moveToThread(MyThread);
                  Mythread->start();
                  connect(this,SIGNAL,obj,SLOT);
                  emit signalStartMyProgressSlot();

                  The code above worked but i have an issue now. I should emit this signal from different files to get the task done by the thread which means i will have to declare the signal in all those files. Is there any other way where in could do it at one place and still get the task done by the thread?

                  Thanks

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    [quote author="raj.qtdev" date="1380561470"]MyObject * obj;
                    Qthread *MyThread = new QThread;
                    obj->moveToThread(MyThread);
                    Mythread->start();
                    connect(this,SIGNAL,obj,SLOT);
                    emit signalStartMyProgressSlot();

                    The code above worked but i have an issue now.[/quote]-In the future, please copy+paste the code that you actually tried. This one has compilation errors.-

                    EDIT: I didn't notice it was copied + pasted from NetZwerg, sorry

                    [quote]I should emit this signal from different files to get the task done by the thread which means i will have to declare the signal in all those files. Is there any other way where in could do it at one place and still get the task done by the thread?[/quote]If you don't want to use signals, you can invoke the slot in the MyObject's thread using "QMetaObject::invokeMethod()":http://qt-project.org/doc/qt-5.1/qtcore/qmetaobject.html#invokeMethod and specifying "Qt::QueuedConnection"

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      NetZwerg
                      wrote on last edited by
                      #10

                      the code above is a pseudocode citation of my post before. Generally you could use the given signal in all classes you need. This requires you to connect the signals correctly also. Another option is to move the call to a another class known to all classes in need of the thread and just call a function in there. But this is almost the same work as just implementing the signal/connect...

                      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