Jakob Schou Jensen
Jakob Schou Jensen

Reputation: 229

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.)

Upvotes: 6

Views: 4404

Answers (3)

Pierluigi
Pierluigi

Reputation: 2294

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.

Upvotes: 5

Losiowaty
Losiowaty

Reputation: 8006

You can add your own SIGNAL which gets emitted as the last thing that your run() method does. Then simply connect it to coreesponding SLOT right before calling connect(...);

Upvotes: 4

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14941

There might be, or you might have to go a slight bit higher-level. The QFuture and QFutureWatcher classes are designed to work with Qt's Concurrent framework, and the QFutureWatcher class has a signal when the item it is watching has finished.

Upvotes: 4

Related Questions