C++ (Qt)// Базовый класс для всех алгоритмовclass Algo : public QObject{ Q_OBJECTpublic: Algo() : QObject() {} virtual void run() = 0; // метод выполняющий алгоритм signals: void progress( int val );}; class BooAlgo : public Algo{ Q_OBJECTpublic: BooAlgo( int param1, float param2 ) : Algo() { ... } virtual void run() { for(;;) { emit progress( 100500 ); } }}; class AlgoRunnable : public QRunnable{public: AlgoRunnable( Algo *algo ) : m_algo( algo ) { Q_ASSERT( m_algo ); setAutoDelete( true ); } virtual void run() { m_algo->moveToThread( QThread::currentThread() ); m_algo->run(); } private: Algo *m_algo;}; // Использовать такBooAlgo *a = new BooAlgo( 10, 20 );connect( a, SIGNAL( progress(int) ), ... ); QThreadPool::globalInstance()->start( new AlgoRunnable( a ) );
QObject::moveToThread: Current thread (0x10ec0f0) is not the object's thread (0xdce0f0).Cannot move to target thread (0x10ec0f0)
C++ (Qt)#ifndef WORKER_H#define WORKER_H #include <QObject>#include <QRunnable> class Worker : public QObject, public QRunnable{ Q_OBJECTpublic: Worker(QObject* parent = NULL);}; #endif // WORKER_H