Qt - how to kill QThread

admin

Administrator
Staff member
I'm trying to kill/cancel a QThread. I followed the implementation of <a href="https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/">https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/</a>

I create the QThread like this:

Code:
workerThread = new QThread();
ImageProcessor* worker = new ImageProcessor();
worker-&gt;moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread-&gt;start();

and try to stop it on a certain UI event by calling:

Code:
workerThread-&gt;quit();

My worker looks like this:

Code:
class ImageProcessor : public QObject
{
Q_OBJECT
public:
explicit ImageProcessor(QObject *parent = 0);
~ImageProcessor();

signals:
void finished();

public slots:
void process();

private:
//...
};

Where process calls an API with heavy calculations

Code:
void ImageProcessor::process()
{
// API call...

emit finished();
}

My problem is, that after calling quit on the thread, the thread keeps running ie the calculations of the API don't stop. Because the worker only calls the API, I cannot work with a cancel check in the worker loop. Any ideas?

Thanks in advance!