QueuedCustomType example: use QThread::requestInterruption

Drop the home-made solution with mutex and bool (which could have been
an atomic, but we have had a ready-made solution in QThread for a long
time).

Pick-to: 6.5 6.6
Change-Id: Id213a021f0ae94215afb28ff874fcb597dd1e6f9
Reviewed-by: MohammadHossein Qanbari <mohammad.qanbari@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mikko Hallamaa <mikko.hallamaa@qt.io>
Reviewed-by: Ed Cooke
This commit is contained in:
Volker Hilsheimer 2023-10-23 13:41:46 +02:00
parent ae39b16345
commit bd5c6fab40
3 changed files with 2 additions and 19 deletions

View File

@ -10,15 +10,10 @@
RenderThread::RenderThread(QObject *parent) RenderThread::RenderThread(QObject *parent)
: QThread(parent) : QThread(parent)
{ {
m_abort = false;
} }
RenderThread::~RenderThread() RenderThread::~RenderThread()
{ {
mutex.lock();
m_abort = true;
mutex.unlock();
wait(); wait();
} }
@ -29,7 +24,6 @@ void RenderThread::processImage(const QImage &image)
return; return;
m_image = image; m_image = image;
m_abort = false;
start(); start();
} }
@ -60,17 +54,10 @@ void RenderThread::run()
const Block block(QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1), const Block block(QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1),
QColor(red/n, green/n, blue/n)); QColor(red/n, green/n, blue/n));
emit sendBlock(block); emit sendBlock(block);
if (m_abort) if (isInterruptionRequested())
return; return;
msleep(10); msleep(10);
} }
} }
} }
//![processing the image (finish)] //![processing the image (finish)]
void RenderThread::stopProcess()
{
mutex.lock();
m_abort = true;
mutex.unlock();
}

View File

@ -5,7 +5,6 @@
#define RENDERTHREAD_H #define RENDERTHREAD_H
#include <QImage> #include <QImage>
#include <QMutex>
#include <QThread> #include <QThread>
class Block; class Block;
@ -20,7 +19,6 @@ public:
~RenderThread(); ~RenderThread();
void processImage(const QImage &image); void processImage(const QImage &image);
void stopProcess();
signals: signals:
void sendBlock(const Block &block); void sendBlock(const Block &block);
@ -29,9 +27,7 @@ protected:
void run(); void run();
private: private:
bool m_abort;
QImage m_image; QImage m_image;
QMutex mutex;
}; };
//! [RenderThread class definition] //! [RenderThread class definition]

View File

@ -29,7 +29,7 @@ Window::Window(QWidget *parent)
connect(loadButton, &QPushButton::clicked, connect(loadButton, &QPushButton::clicked,
this, QOverload<>::of(&Window::loadImage)); this, QOverload<>::of(&Window::loadImage));
connect(resetButton, &QPushButton::clicked, connect(resetButton, &QPushButton::clicked,
thread, &RenderThread::stopProcess); thread, &RenderThread::requestInterruption);
connect(thread, &RenderThread::finished, connect(thread, &RenderThread::finished,
this, &Window::resetUi); this, &Window::resetUi);
//! [set up widgets and connections] //! [connecting signal with custom type] //! [set up widgets and connections] //! [connecting signal with custom type]