#ifndef RUNCONTROLLABLETASK_H #define RUNCONTROLLABLETASK_H /** From answer by Hatter * * https://stackoverflow.com/questions/5423058/qfuture-that-can-be-cancelled-and-report-progress */ #include #include #include #include "ControllableTask.h" #include "TaskControl.h" template class RunControllableTask : public QFutureInterface , public QRunnable { public: RunControllableTask(ControllableTask* tsk) : task(tsk) { } virtual ~RunControllableTask() { delete task; } QFuture start() { this->setRunnable(this); this->reportStarted(); QFuture future = this->future(); QThreadPool::globalInstance()->start(this, /*m_priority*/ 0); return future; } void run() { if (this->isCanceled()) { this->reportFinished(); return; } TaskControl control(this); try { result = this->task->run(control); if (!this->isCanceled()) { this->reportResult(result); } } catch (QException &e) { reportException(e); } catch (...) { reportException(QUnhandledException()); } this->reportFinished(); } private: T result; ControllableTask *task; }; #endif // RUNCONTROLLABLETASK_H