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. How to clear plot in QCustomPlot?
Forum Update on Monday, May 27th 2025

How to clear plot in QCustomPlot?

Scheduled Pinned Locked Moved Solved General and Desktop
qcustomplotplotvectorqt5.15.2
2 Posts 1 Posters 522 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.
  • T Offline
    T Offline
    timob256
    wrote on 30 Jul 2023, 17:09 last edited by timob256
    #1

    I display a graph of 80 positions, but when I print it, all 100 positions are reloaded.
    Tried to use wGraphic->graph(0)->clearData(); did nothing, and wGraphic->clearPlottables(); wGraphic->clearGraphs(); this generally led to a memory error.

    .h

    ...
        QCustomPlot *wGraphic;      // Объявляем объект QCustomPlot
       ...
        QVector <double> x; // пока так размеры выставим
        QVector <double> y;
    

    .срр

        // Инициализируем объект полотна для графика ...
        wGraphic = new QCustomPlot();
        ui->gridLayout->addWidget(wGraphic,2,0,5,5); // ... и устанавливаем
    ...
     // Добавляем график на полотно
        wGraphic->addGraph(wGraphic->xAxis, wGraphic->yAxis);
    
        // Подписываем оси координат
        wGraphic->xAxis->setLabel("итерации");
        wGraphic->yAxis->setLabel("Дб");
    
        // Устанавливаем максимальные и минимальные значения координат
        wGraphic->xAxis->setRange(0,100);
        wGraphic->yAxis->setRange(-50,50);
    
        // Отрисовываем содержимое полотна
        wGraphic->replot();
    
        // добавляем красную линию на график
        wGraphic->addGraph(); // red line
        wGraphic->graph(0)->setPen(QPen(QColor(255, 110, 40)));
    ...
        // тут размеры отрис векторов
        x.reserve(80);
        y.reserve(80);
        // устанавливаем одно значение для всех элементов
        x.fill(0);
        y.fill(0);
    ...
        x << str_x.toDouble();
        y << str_y.toDouble();
    
    //    qDebug() << "str_priem: " << str_priem;
    
    
        qDebug() << "str_x: " << str_x;
        qDebug() << "str_y: " << str_y;
    
    //    wGraphic->graph(0)->clearData();
    
    
        wGraphic->graph(0)->setData(y,x);     // Устанавливаем координаты точек графика
    
    //        wGraphic->clearPlottables(); выдаёт ошибку памяти
    //    wGraphic->clearGraphs(); выдаёт ошибку памяти
    
        // Отрисовываем содержимое полотна
        wGraphic->replot();
    
        //  ???
        wGraphic->update();
    
        // Очищаем принятую строку
        str_priem.clear();
    

    введите сюда описание изображения

    I wish there was something like this

    введите сюда описание изображения

    T 1 Reply Last reply 8 Aug 2023, 20:14
    0
    • T timob256
      30 Jul 2023, 17:09

      I display a graph of 80 positions, but when I print it, all 100 positions are reloaded.
      Tried to use wGraphic->graph(0)->clearData(); did nothing, and wGraphic->clearPlottables(); wGraphic->clearGraphs(); this generally led to a memory error.

      .h

      ...
          QCustomPlot *wGraphic;      // Объявляем объект QCustomPlot
         ...
          QVector <double> x; // пока так размеры выставим
          QVector <double> y;
      

      .срр

          // Инициализируем объект полотна для графика ...
          wGraphic = new QCustomPlot();
          ui->gridLayout->addWidget(wGraphic,2,0,5,5); // ... и устанавливаем
      ...
       // Добавляем график на полотно
          wGraphic->addGraph(wGraphic->xAxis, wGraphic->yAxis);
      
          // Подписываем оси координат
          wGraphic->xAxis->setLabel("итерации");
          wGraphic->yAxis->setLabel("Дб");
      
          // Устанавливаем максимальные и минимальные значения координат
          wGraphic->xAxis->setRange(0,100);
          wGraphic->yAxis->setRange(-50,50);
      
          // Отрисовываем содержимое полотна
          wGraphic->replot();
      
          // добавляем красную линию на график
          wGraphic->addGraph(); // red line
          wGraphic->graph(0)->setPen(QPen(QColor(255, 110, 40)));
      ...
          // тут размеры отрис векторов
          x.reserve(80);
          y.reserve(80);
          // устанавливаем одно значение для всех элементов
          x.fill(0);
          y.fill(0);
      ...
          x << str_x.toDouble();
          y << str_y.toDouble();
      
      //    qDebug() << "str_priem: " << str_priem;
      
      
          qDebug() << "str_x: " << str_x;
          qDebug() << "str_y: " << str_y;
      
      //    wGraphic->graph(0)->clearData();
      
      
          wGraphic->graph(0)->setData(y,x);     // Устанавливаем координаты точек графика
      
      //        wGraphic->clearPlottables(); выдаёт ошибку памяти
      //    wGraphic->clearGraphs(); выдаёт ошибку памяти
      
          // Отрисовываем содержимое полотна
          wGraphic->replot();
      
          //  ???
          wGraphic->update();
      
          // Очищаем принятую строку
          str_priem.clear();
      

      введите сюда описание изображения

      I wish there was something like this

      введите сюда описание изображения

      T Offline
      T Offline
      timob256
      wrote on 8 Aug 2023, 20:14 last edited by
      #2

      @timob256 In general , there was one mistake , for some reason I thought that by setting the size of the vector , it would correct everything itself , but it turned out not to be so

       void MainWindow::slotProcessDatagrams()
      {
          QByteArray baDatagram_in;
          do {
              baDatagram_in.resize(m_pudp_in->pendingDatagramSize());
              m_pudp_in->readDatagram(baDatagram_in.data(), baDatagram_in.size());
          } while(m_pudp_in->hasPendingDatagrams());
      
          QDataStream in(&baDatagram_in, QIODevice::ReadOnly);
          in.setVersion(QDataStream::Qt_5_3);
      
          in >>  str_priem; // принимаем сообщение
      
          str_x = str_priem.section(' ',0,0);
          str_y = str_priem.section(' ',1);
      
          if(my_int >= 80)
          {
              // clear firs element
      //        x.pop_back();
      //        y.pop_back();
      //        x.removeLast();
      //        y.removeLast();
              x.pop_front(); //последний из вектора 
              y.pop_front();
      
              x << str_x.toDouble();
              y << str_y.toDouble();
          }
          else
          {
              x << str_x.toDouble();
              y << str_y.toDouble();
              my_int = my_int + 1;
          }
      
          // чистим старое отрисовываем новое
          wGraphic->graph(0)->clear();
          wGraphic->graph(0)->rescaleAxes();
      
      //    wGraphic->replot();     // Отрисовываем содержимое полотна
          wGraphic->graph(0)->setData(y,x);     // Устанавливаем координаты точек графика
      
          wGraphic->replot();     // Отрисовываем содержимое полотна
      
          // Очищаем принятую строку
          str_priem.clear();
      }
      

      True, a new error jumped (for some reason the last and first point somehow connects, I don't know how to break)

      введите сюда описание изображения

      1 Reply Last reply
      0
      • T timob256 has marked this topic as solved on 8 Aug 2023, 20:14

      • Login

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