Added some classes from a stackoverflow to utilize Qt's concurrency functions especially cancellable QFuture's
This commit is contained in:
parent
ad3f605ada
commit
99d738ee65
5 changed files with 119 additions and 1 deletions
45
core/RunControllableTask.h
Normal file
45
core/RunControllableTask.h
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#ifndef RUNCONTROLLABLETASK_H
|
||||
#define RUNCONTROLLABLETASK_H
|
||||
/** From answer by Hatter
|
||||
*
|
||||
* https://stackoverflow.com/questions/5423058/qfuture-that-can-be-cancelled-and-report-progress
|
||||
*/
|
||||
|
||||
#include <QFutureInterface>
|
||||
#include <QRunnable>
|
||||
#include "ControllableTask.h"
|
||||
|
||||
template <typename T>
|
||||
class RunControllableTask : public QFutureInterface<T> , public QRunnable
|
||||
{
|
||||
public:
|
||||
RunControllableTask(ControllableTask<T>* tsk) : task(tsk) { }
|
||||
virtial ~RunControllableTask() { delete task; }
|
||||
|
||||
QFuture<T> start()
|
||||
{
|
||||
this->setRunnable(this);
|
||||
this->reportStarted();
|
||||
QFuture<T> future = this->future();
|
||||
QThreadPool::globalInstance()->start(this, /*m_priority*/ 0);
|
||||
return future;
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
if (this->isCanceled()) {
|
||||
this->reportFinished();
|
||||
return;
|
||||
}
|
||||
TaskControl control(this);
|
||||
result = this->task->run(control);
|
||||
if (!this->isCanceled()) {
|
||||
this->reportResult(result);
|
||||
}
|
||||
this->reportFinished();
|
||||
}
|
||||
|
||||
T result;
|
||||
ControllableTask<T> *task;
|
||||
};
|
||||
#endif // RUNCONTROLLABLETASK_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue