title: QT C++ How to return QImage from QThread and display it on QLabel in QMainWindow?
body: <p>I am trying to process some image using OpenCV Library inside a thread, because the process operation takes some time before it's done.</p>
  <p>So the problem is QThread always returning a Null QImage to the Slot in QMainWindow.</p>
  <p>I get this Exception error:</p>
  <pre><code>Exception thrown at 0x00007FFE01962F6D (Qt5Guid.dll) in QtWidgetsApplication1.exe: 0xC0000005: Access violation reading location 0x0000022CAB6EE080.
  </code></pre>
  <p>Error occurs in this file:</p>
  <p><strong>qtwidgetsapplication1.cpp</strong> this file is for QMainWindow</p>
  <pre><code>#include &quot;qtwidgetsapplication1.h&quot;
  #include &quot;stdafx.h&quot;
  
  
  QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent)
      : QMainWindow(parent)
  {
      ui.setupUi(this); 
      
      connect(ui.addItem_btn, SIGNAL(clicked()), this, SLOT(addItem_btn_OnClick())); // Add Item to the List
      
      dftThread = new DetectFaceThread(this);
  
      connect(dftThread, &amp;DetectFaceThread::detectedFace, this, &amp;QtWidgetsApplication1::onDetectedFace);
      connect(dftThread, &amp;DetectFaceThread::finished, dftThread, &amp;QObject::deleteLater);
  }
  
  QtWidgetsApplication1::~QtWidgetsApplication1()
  {
      
  }
  
  void QtWidgetsApplication1::addItem_btn_OnClick()
  {
      dftThread-&gt;start();
  }
  
  void QtWidgetsApplication1::onDetectedFace(const QImage&amp; face)
  {
      
      if (face.isNull())
      {
          QMessageBox::warning(this, QString(&quot;Detection Error&quot;), QString(&quot;Face not detected!&quot;));
          return;
      }
      ui.imgDisplay_label-&gt;setPixmap(QPixmap::fromImage(face));
  }
  </code></pre>
  <p>Here is my Code:</p>
  <p><strong>DetectFaceThread.h</strong></p>
  <pre><code>#pragma once
  #include &lt;qthread.h&gt;
  #include &lt;QtWidgets/qmessagebox.h&gt;
  #include &lt;qmutex.h&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/objdetect.hpp&gt;
  
  
  class DetectFaceThread :
      public QThread
  {
      Q_OBJECT
  
  public:
      DetectFaceThread(QWidget* parent = nullptr);
      ~DetectFaceThread();
      void run() override;
  
  signals:
      void detectedFace(const QImage &amp;face);
  };
  </code></pre>
  <p><strong>DetectFaceThread.cpp</strong></p>
  <pre><code>#include &quot;DetectFaceThread.h&quot;
  
  DetectFaceThread::DetectFaceThread(QWidget* parent)
  {
  
  }
  
  DetectFaceThread::~DetectFaceThread()
  {
      QMessageBox::information(nullptr, QString(&quot;Thread Info&quot;), QString(&quot;Thread successfully destroyed&quot;));
  }
  
  void DetectFaceThread::run()
  {
      QMutex mutex;
  
      mutex.lock();
  
      std::string img_path = &quot;res/paper.jpg&quot;;
      cv::Mat img = cv::imread(img_path);
  
      if (img.empty())
      {
          QMessageBox::warning(nullptr, QString(&quot;Load Error&quot;), QString(&quot;Image not found!&quot;));
          return;
      }
  
      cv::cvtColor(img, img, cv::ColorConversionCodes::COLOR_BGR2RGB);
  
      float w = 800, h = 1000;
  
      cv::Point2f src[4] = { {383, 445}, {885, 521}, {89, 1125}, {921, 1270} };
      cv::Point2f dst[4] = { {0.0f, 0.0f}, {w, 0.0f}, {0.0f, h}, {w, h} };
  
      cv::Mat matrix = getPerspectiveTransform(src, dst);
      cv::Mat img_warp;
      cv::warpPerspective(img, img_warp, matrix, cv::Size(w, h));
  
      QImage qimage(img_warp.data, img_warp.cols, img_warp.rows, img_warp.step, QImage::Format::Format_RGB888);
  
      mutex.unlock();
  
      emit detectedFace(qimage);
  }
  </code></pre>
  <p>And in the end the application just terminates, can any one help me please.</p>
  <p><strong>Update:</strong> I tried your solution but it throws the same Exception error.</p>
  <pre><code>connect(dftThread, &amp;DetectFaceThread::detectedFace, this, &amp;QtWidgetsApplication1::onDetectedFace, Qt::QueuedConnection);
  </code></pre>