title: QImage is not passing through signal-slots Qt
body: <p>I am building an application which gets the image from an IP camera using OpenCV in one thread, then after processing it converts the <code>Mat</code> to <code>QImage</code>, then emits the <code>QImage</code> to the signal, and then the image will be received in the GUI thread by the corresponding slots.</p>
  
  <p>signal declares in the processing image thread class:</p>
  
  <pre><code>void sendImage(const QImage &amp;frame);
  </code></pre>
  
  <p>slot declares in the GUI thread class:</p>
  
  <pre><code>void onGetImage(const QImage &amp;img);
  </code></pre>
  
  <p>but after that when I show the image on <code>QLabel</code> or saving the image to the disk, the program crashes.</p>
  
  <p>I am a little new to <code>C++</code>, so is there any problem with the passing reference of the <code>QImage</code> object? If yes, then what is the correct way to pass <strong>Objects</strong> between threads without any copying (reallocating memory) overhead? Because I want to prevent even a small overhead in a long time running application.</p>
  
  <p><strong>Update:</strong> Sometimes run for some while, and then crashes, sometimes it crashes even in the first frame.</p>
  
  <p>Thanks!</p>
  
  <p><strong>Code: which process the image</strong></p>
  
  <pre><code>class ImageProcess
  {
  public:
      ImageProcess(std::function&lt;void(cv::Mat&amp;)&gt; imgCallback):imgCallback(imgCallback){}
  public:
      void start()
      {
          while(start) // loop till start become false
          {
              videpCapture.read(frame);
              // .... some process on image
              imgCallback(frame)
          }
      }
  private:
      cv::Mat frame;
      std::function&lt;void(cv::Mat&amp;)&gt; imgCallback;
  };
  </code></pre>
  
  <p><strong>code: which communicate between image process unit and the GUI</strong></p>
  
  <pre><code>class WorkerThread: public QObject
  {
      Q_OBJECT
  public:
      WorkerThread()
      {
           imgProcess = new ImageProcess(std::bind(.....)); // bind the callback
      }
  
  public slots:
      void onStart()
      {
          if(imProcessThread != nullptr)
          {
              imgProcess.start = false; // stop if running
              imProcessThread-&gt;quit(); // quit the thread
              imProcessThread-&gt;wait(); // wait to finish
              delete imProcessThread; // delete the pointer
          }
          imProcessThread = new ImageProcessThread(imgProcess); // create
          imProcessThread-&gt;start(); // start thread
      }
  signals:
      void sendMessage(const QString&amp; msg, const int&amp; code);
  private:
      ImageProcess *imgProcess;
      void frameCallback(const cv::Mat&amp; frame); // frame callback
      {
          emit sendImage(matToQImage(frame)); // send the image to UI
      }
      // Mat to QImage converter
      QImage matToQImage(const cv::Mat &amp;mat)
      {
          cv::Mat rgbMat;
          if(mat.channels() == 1) { // if grayscale image
              return QImage((uchar*)mat.data, mat.cols, mat.rows, (int)mat.step, 
      QImage::Format_Indexed8);// declare and return a QImage
          } else if(mat.channels() == 3) { // if 3 channel color image
              cv::cvtColor(mat, rgbMat, CV_BGR2RGB); // invert BGR to RGB
              return QImage((uchar*)rgbMat.data, mat.cols, mat.rows, 
      (int)mat.step, QImage::Format_RGB888);// declare and return a QImage
          }
          return QImage();
      }
      // image process thread
      class ImageProcessThread : public QThread
      {
      public:
          ImageProcessThread(ImageProcess *ip) : ip(ip){}
      protected:
          void run()
          {
              ip-&gt;start();
          }
      private:
          ImageProcess *ip;
      } *imProcessThread = nullptr;
  };
  </code></pre>
  
  <p><strong>code: UI</strong> </p>
  
  <pre><code>class Camera : public QWidget
  {
      Q_OBJECT
  public:
      explicit Camera(QWidget *parent = 0)
      {
          wt = new WorkerThread;
          thread = new QThread;
          //conncet the signal-slots
          connect(wt, &amp;WorkerThread::sendImage, this, &amp;Camera::onGetImage);
          connect(ui-&gt;btStart, &amp;QPushButton::clicked, wt, &amp;WorkerThread::start);
      }
  private slots:
      void onGetImage(const QImage &amp;img)
      {
          // set the image to QLabel
      }
  private:
      WorkerThread *wt;
      QThread *thread;
  };
  wt.moveToThread(thread);
  thread-&gt;start();
  </code></pre>
  
  <p><strong>So what can be a goog design for this purpose?</strong></p>