Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update QChart in real time (QtWidgets)

Update QChart in real time (QtWidgets)

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.7k 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello!!

    I am trying to use an histogram associated to a video streaming so, I want to update the chart frame by frame.
    I have a QPushButton associated to a signal wich shows the histogram with this slot:

    void MainWindow::createHistogram(bool status) {
        if (status) {
            showHist = true;
    
            chartHist = new QChart();
    
            int histSize[1] = {256}; // number of bins
            float hranges[2] = {0.0, 255.0}; // min andax pixel value
            const float* ranges[1] = {hranges};
            int channels[1] = {0}; // only 1 channel used
    
            cv::MatND hist;
    
            cv::Mat ret;
            imageFunctions::qimage_to_mat(imageViewer->item->pixmap().toImage(), ret);
    
            cv::calcHist(&ret, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
    
            series = new QtCharts::QBarSet("");
    
            for (int h = 0; h < histSize[0]; ++h) {
                float bin_value = hist.at<float>(h);
                series->append(bin_value);
                if (valueHistMax < bin_value) valueHistMax = bin_value;
            }
    
            QPen pen(0x09418B);
            pen.setWidth(3);
            series->setPen(pen);
    
            QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
            barseries->append(series);
    
            chartHist->removeAllSeries();
            chartHist->addSeries(barseries);
            chartHist->setAnimationOptions(QChart::SeriesAnimations);
            chartHist->legend()->setVisible(false);
            chartHist->setMaximumWidth(100);
            chartHist->setMargins(QMargins(0,0,4,0));
            chartHist->setAcceptHoverEvents(true);
            chartHist->layout()->setContentsMargins(0,0,0,0);
            chartHist->setBackgroundRoundness(0);
    
            ui->histLayout->addWidget(chartViewHist);
        } else {
            delete series;
            delete chartHist;
            delete chartViewHist;
            showHist = false;
        }
    }
    

    And when I receive the next frame I call this function:

    void MainWindow::updateHistogram(cv::Mat *image) {
    
        int histSize[1] = {256}; // number of bins
        float hranges[2] = {0.0, 255.0}; // min andax pixel value
        const float* ranges[1] = {hranges};
        int channels[1] = {0}; // only 1 channel used
    
        cv::MatND hist;
        cv::calcHist(image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
    
        //series = new QtCharts::QBarSet("");
    
        for (int h = 0; h < histSize[0]; ++h) {
            float bin_value = hist.at<float>(h);
            series->replace(h, bin_value);
            //series->append(bin_value);
            if (valueHistMax < bin_value) valueHistMax = bin_value;
        }
    
        QPen pen(0x09418B);
        pen.setWidth(3);
        series->setPen(pen);
    
        QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
        barseries->append(series);
    
        chartHist->removeAllSeries();
        chartHist->addSeries(barseries);
    
        //clearLayout(ui->histLayout);
        //ui->histLayout->addWidget(chartViewHist);
    }
    

    But this gives me a memory access error. I don't know how to do it. What could be wrong here?

    Thank you very much!!

    jsulmJ 1 Reply Last reply
    0
    • ivanicyI ivanicy

      Hello!!

      I am trying to use an histogram associated to a video streaming so, I want to update the chart frame by frame.
      I have a QPushButton associated to a signal wich shows the histogram with this slot:

      void MainWindow::createHistogram(bool status) {
          if (status) {
              showHist = true;
      
              chartHist = new QChart();
      
              int histSize[1] = {256}; // number of bins
              float hranges[2] = {0.0, 255.0}; // min andax pixel value
              const float* ranges[1] = {hranges};
              int channels[1] = {0}; // only 1 channel used
      
              cv::MatND hist;
      
              cv::Mat ret;
              imageFunctions::qimage_to_mat(imageViewer->item->pixmap().toImage(), ret);
      
              cv::calcHist(&ret, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
      
              series = new QtCharts::QBarSet("");
      
              for (int h = 0; h < histSize[0]; ++h) {
                  float bin_value = hist.at<float>(h);
                  series->append(bin_value);
                  if (valueHistMax < bin_value) valueHistMax = bin_value;
              }
      
              QPen pen(0x09418B);
              pen.setWidth(3);
              series->setPen(pen);
      
              QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
              barseries->append(series);
      
              chartHist->removeAllSeries();
              chartHist->addSeries(barseries);
              chartHist->setAnimationOptions(QChart::SeriesAnimations);
              chartHist->legend()->setVisible(false);
              chartHist->setMaximumWidth(100);
              chartHist->setMargins(QMargins(0,0,4,0));
              chartHist->setAcceptHoverEvents(true);
              chartHist->layout()->setContentsMargins(0,0,0,0);
              chartHist->setBackgroundRoundness(0);
      
              ui->histLayout->addWidget(chartViewHist);
          } else {
              delete series;
              delete chartHist;
              delete chartViewHist;
              showHist = false;
          }
      }
      

      And when I receive the next frame I call this function:

      void MainWindow::updateHistogram(cv::Mat *image) {
      
          int histSize[1] = {256}; // number of bins
          float hranges[2] = {0.0, 255.0}; // min andax pixel value
          const float* ranges[1] = {hranges};
          int channels[1] = {0}; // only 1 channel used
      
          cv::MatND hist;
          cv::calcHist(image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
      
          //series = new QtCharts::QBarSet("");
      
          for (int h = 0; h < histSize[0]; ++h) {
              float bin_value = hist.at<float>(h);
              series->replace(h, bin_value);
              //series->append(bin_value);
              if (valueHistMax < bin_value) valueHistMax = bin_value;
          }
      
          QPen pen(0x09418B);
          pen.setWidth(3);
          series->setPen(pen);
      
          QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
          barseries->append(series);
      
          chartHist->removeAllSeries();
          chartHist->addSeries(barseries);
      
          //clearLayout(ui->histLayout);
          //ui->histLayout->addWidget(chartViewHist);
      }
      

      But this gives me a memory access error. I don't know how to do it. What could be wrong here?

      Thank you very much!!

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ivanicy said in Update QChart in real time (QtWidgets):

      memory access error

      Where exactly? Did you try to debug? Did you check the stack trace when it crashes?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      ivanicyI 1 Reply Last reply
      0
      • jsulmJ jsulm

        @ivanicy said in Update QChart in real time (QtWidgets):

        memory access error

        Where exactly? Did you try to debug? Did you check the stack trace when it crashes?

        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        @jsulm I don't know where. Is one of these times that appears an hexadecimal window, Qt doesn't show where is it crashed:

        0_1516967482301_fc1452c1-c6b7-4f1d-845f-27e379b58f81-imagen.png

        I have this lines in the MainWindow constructor:

            chartHist = new QChart();
            chartViewHist = new QChartView(chartHist);
            chartViewHist->setRenderHint(QPainter::Antialiasing);
            chartViewHist->setMaximumWidth(100);
        

        I am wondering that it is crashing when I do the instantiation of the objects but I don't know.

        jsulmJ 1 Reply Last reply
        0
        • ivanicyI ivanicy

          @jsulm I don't know where. Is one of these times that appears an hexadecimal window, Qt doesn't show where is it crashed:

          0_1516967482301_fc1452c1-c6b7-4f1d-845f-27e379b58f81-imagen.png

          I have this lines in the MainWindow constructor:

              chartHist = new QChart();
              chartViewHist = new QChartView(chartHist);
              chartViewHist->setRenderHint(QPainter::Antialiasing);
              chartViewHist->setMaximumWidth(100);
          

          I am wondering that it is crashing when I do the instantiation of the objects but I don't know.

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ivanicy Are you testing with a release build? Build in debug mode and try again.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          ivanicyI 1 Reply Last reply
          0
          • jsulmJ jsulm

            @ivanicy Are you testing with a release build? Build in debug mode and try again.

            ivanicyI Offline
            ivanicyI Offline
            ivanicy
            wrote on last edited by
            #5

            @jsulm I test in debug mode, and appears that. I was debugging line by line and only works the first time. The next calls of the function don't work.

            1 Reply Last reply
            0

            • Login

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