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

Is there any way to detect when a QT QRunnable object is made? (Except for manually creating some signal event at the end of the run () method.)

+4
source share
3 answers

Perhaps, perhaps, or you may need a slightly higher level. The QFuture and QFutureWatcher classes are designed to work with Qt Parallel structure , and the QFutureWatcher class has a signal when the element that it is viewing has finished.

+4
source

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

+3
source

You can simply use QtConcurrent to run runnable and use QFuture to wait for completion.

 #include <QtConcurrentRun> class MyRunnable : public Runnable{ void run(); } 

to execute 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 launch a new thread to execute the run () method on the instance and immediately return a QFuture that tracks the progress of the instance.

Please note that when using this solution, you are responsible for storing the instance in memory at run time.

+3
source

All Articles