Qt thread which will run multiple times for application life time
-
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
-
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?
-
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.
-
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(); -
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
-
thanks NetZwerg...will give that a try
-
[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.
-
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
-
[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"
-
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...