#ifndef BACKGROUNDTHREAD_H #define BACKGROUNDTHREAD_H #include #include #include class BackgroundThreadBase : public QThread { Q_OBJECT public: BackgroundThreadBase(QObject* parent = 0) : QThread(parent) {} virtual ~BackgroundThreadBase() {} signals: void Initialised(); }; template class BackgroundThread : public BackgroundThreadBase { public: BackgroundThread(QObject* parent = 0); virtual ~BackgroundThread(); boost::shared_ptr Worker() const { return worker_; } protected: void run(); private: boost::shared_ptr worker_; }; template BackgroundThread::BackgroundThread(QObject *parent) : BackgroundThreadBase(parent) { } template BackgroundThread::~BackgroundThread() { if (isRunning()) { quit(); if (wait(10000)) return; terminate(); wait(10000); } } template void BackgroundThread::run() { worker_.reset(new T); emit Initialised(); exec(); worker_.reset(); } #endif // BACKGROUNDTHREAD_H