Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. A bug ghost!
Forum Update on Monday, May 27th 2025

A bug ghost!

Scheduled Pinned Locked Moved 3rd Party Software
opencvqmlc++plugin
2 Posts 2 Posters 1.5k Views
  • 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.
  • D Offline
    D Offline
    Duino
    wrote on 28 Apr 2015, 07:55 last edited by
    #1

    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.

    J 1 Reply Last reply 28 Apr 2015, 08:18
    0
    • D Duino
      28 Apr 2015, 07:55

      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! :)

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 28 Apr 2015, 08:18 last edited by
      #2

      @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
      0

      1/2

      28 Apr 2015, 07:55

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved