Код: Выделить всё
#include
#include
#include
#include
#include
#include
class Producer : public QObject {
Q_OBJECT
public:
Producer(QObject *parent = nullptr) : QObject(parent) {}
public slots:
void produce() {
constexpr auto count = 1000;
for (int i = 0; i < count; ++i) {
QImage img(2448, 2048, QImage::Format_Grayscale8);
img.fill(0);
emit imageReady(img);
}
}
signals:
void imageReady(QImage image);
};
class Consumer : public QObject {
Q_OBJECT
public:
Consumer(QObject *parent = nullptr) : QObject(parent) {}
int consumedCount() const { return count_; }
public slots:
void onImageReady(QImage image) {
QFuture future = QtConcurrent::run([=] {
QImage copy = image.copy(); // Make a deep copy first
QThread::msleep(200); // Mock detection on the copy
qDebug()
Подробнее здесь: [url]https://stackoverflow.com/questions/78242705/queued-async-operations-with-qtconcurrent-interfere-qimage-from-freed[/url]