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 m开发者_StackOverflow中文版anually creating some signalling event at the end of the run() method.)
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.
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.
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(...);
精彩评论