Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect when a QT QRunnable object is done?

Is there a way to detect when a QT QRunnable object is done? (Other than manually creating some signalling event at the end of the run() method.)

like image 248
Jakob Schou Jensen Avatar asked Oct 14 '10 07:10

Jakob Schou Jensen


People also ask

How do you stop QRunnable?

What you want to do is make your background task a QObject, give it a run() slot, and connect the thread. started to the worker. run. That's what will kick the process off.

What is QRunnable for?

The QRunnable class is an interface for representing a task or piece of code that needs to be executed, represented by your reimplementation of the run() function. You can use QThreadPool to execute your code in a separate thread.


1 Answers

You can simply use QtConcurrent to run the runnable and use a QFuture to wait for finished.

#include <QtConcurrentRun>

class MyRunnable : public Runnable{
  void run();
}

in order to run and wait for it you can do the following

//create a new MyRunnable to use
MyRunnable instance;

//run the instance asynchronously
QFuture<void> future = QtConcurrent::run(&instance, &MyRunnable::run);

//wait for the instance completion
future.waitForFinished();

Qtconcurrent::run will start a new thread to execute method run() on instance and immediately returns a QFuture that tracks instance progress.

Notice however that using this solution you are responsable to keep instance in memory during the execution.

like image 125
Pierluigi Avatar answered Sep 21 '22 13:09

Pierluigi