Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    A bug ghost!

    3rd Party Software
    opencv qml c++plugin
    2
    2
    1244
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Duino
      Duino last edited by

      I just meet a bug ghost in my code.That means if I try to find what happen inside,the code runs well.But if I don't, bugs happen.
      The code is trying to draw a histogram of an image using OpenCV functions and show it in qml.Here is the code.

      C++ code

      "histogram.h"
      #include <QtQuick/QQuickPaintedItem>
      #include <QImage>
      #include <QPainter>

      #include <opencv2/core/core.hpp>
      #include <opencv2/imgproc/imgproc.hpp>
      #include <opencv2/highgui/highgui.hpp>

      using namespace cv;

      class Histogram : public QQuickPaintedItem{

      Q_OBJECT
      Q_PROPERTY(QList<int> hist READ hist WRITE setHist)
      

      public:

      Histogram(QQuickItem *parent = 0);
      ~Histogram();
      void paint(QPainter *painter);
      QList<int> hist()const { return m_hist; }
      void setHist(const QList<int> arg);
      

      private:

      QList<int> m_hist;
      QImage histPainter;
      

      };

      "histogram.cpp"

      #include "histogram.h"
      Histogram::Histogram(QQuickItem *parent):
      QQuickPaintedItem(parent)
      {
      }
      Histogram::~Histogram()
      {
      }
      void Histogram::paint(QPainter *painter)
      {

      if(histPainter.isNull()){
          qDebug() << "histPainter is NULL!";
          return;
      }
      painter->drawImage(QPoint(0,0), histPainter);
      

      }
      void Histogram::setHist(const QList<int> arg)
      {

      if(arg.length() <=1)
      {
          qDebug() << "no Hist from QML!";
          return;
      }
      m_hist = arg;
      Mat histImage = Mat::ones(200, 320, CV_8UC3)*255;
      histImage = Scalar::all(200);
      int histSize = 320;
      int binW = cvRound((double)histImage.cols/histSize);
      for( int i = 0; i < histSize; i++ )
          rectangle( histImage, Point(i*binW, histImage.rows),
                     Point((i+1)*binW, histImage.rows - m_hist.at(i)),
                     Scalar::all(0), -1, 8, 0 );
      histPainter  = QImage((unsigned char*)histImage.data,
              histImage.cols, histImage.rows,
              histImage.step,
              QImage::Format_RGB888);
      
      //try to find what happened......
      //histPainter.save("dumin.jpg");
      //histPainter.load("dumin.jpg");
      
      update();
      

      }

      QML code

          Histogram{
              id: histogram
              width: 320; height: 200
          }
      

      If I use" histPainter.save("dumin.jpg"); histPainter.load("dumin.jpg");",it can show the histogram well.But if I comment the code out, it just shows something strange and not stable,just like a ghost.

      How should I catch this bug ghost???
      Thank you for reading my question! :)

      Just enjoy coding.

      JKSH 1 Reply Last reply Reply Quote 0
      • JKSH
        JKSH Moderators @Duino last edited by

        @Duino said:

        I just meet a bug ghost in my code.That means if I try to find what happen inside,the code runs well.But if I don't, bugs happen.

        It's not a ghost. It's a Heisenbug :)

        How should I catch this bug ghost???

        Simplify your program, do unit testing. and use a tool like Valgrind.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply Reply Quote 0
        • First post
          Last post