http://doc.qt.io/qt-5/qthread.html
class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
public:
Controller() {
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
My way:
class Controller: public QObject
{
Q_OBJECT
public:
Worker objWorker;
QThread objQThread;
Controller();
~Controller();
public slots:
void receiveImage();
};
and
Controller::Controller()
{
objWorker.moveToThread( &objQThread );
connect( &objWorker, &Worker::imageReady, this, &Controller::receiveImage );
objQThread.start();
}
Is their method better than mine in some way? Which should be preferred in which case?
From: http://doc.qt.io/qt-5/qthread.html
From Qt 4.8 onwards, it is possible to deallocate objects that live in a thread that has just ended, by connecting the finished() signal to QObject::deleteLater().
This can be achieved only by making a pointer object of the Worker class and allocating memory to it.