QCustomPlot Discussion and Comments

Crash during reascaling and update dataReturn to overview

Hi,

I want to use QCustomPlot, to visualize data. So far no problem. But as soon as the user wants to modify these data or simply refresh these data my program crashes. I use qt5.12.7, mingz7.03 and I'm on windows 10. Do you have an idea of the problem

in advance thank you

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qcustomplot.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    virtual ~MainWindow() {}
    void onEdit();
private:
     QVector<double> x;
     QVector<double> y;
     QCustomPlot *custom;
};
#endif // MAINWINDOW_H

and mainwindow.cpp

#include "mainwindow.h"
#include <QPushButton>
#include <QtMath>
#include <QVBoxLayout>


using namespace std;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

QPushButton *bouton = new QPushButton("Edit Signal", this);

QObject::connect(bouton, &QPushButton::clicked, this, &MainWindow::onEdit);

int n = 50;

x.resize(n);
y.resize(n);
for (int i=0; i<n; i++)
{
  x[i] = (double(i)/double(n)+0.5)*5 + 0.5;
  y[i] = (qSin(x[i]*3)*qSin(qCos(x[i]*0.7)*2)+4*qCos(qSin(x[i])*2))*2 + 0.3;
}

QCustomPlot *custom = new QCustomPlot(this);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(custom);
layout->addWidget(bouton);
QWidget *widget = new QWidget(this);
widget->setLayout(layout);
this->setCentralWidget(widget);

custom->addGraph();
custom->graph(0)->setData(x, y);
custom->rescaleAxes();
custom->replot();
}

void MainWindow::onEdit(){
    y[0] = 5;
    y[5]= -1;
    custom->graph(0)->setData(x, y);
    custom->rescaleAxes();
    custom->replot();
}

Sylm

Hello,

Have you set an range ?


    
void MainWindow::onEdit(){
    y[0] = 5;
    y[5]= -1;

double xmin  = *std::min_element(x.constBegin(), xx.constEnd());
    double xmax = *std::max_element(x.constBegin(), x.constEnd());
    double ymin  = *std::min_element(y.constBegin(), y.constEnd());
    double ymax = *std::max_element(y.constBegin(),y.constEnd());

    custom->xAxis->setRange(xmin, xmax);
    custom->yAxis->setRange(ymin, ymax);
custom->graph(0)->setData(x, y);
....

Hello,

Same behavior it crashes when I press the button. the error is SEGMENTATION FAULT with a list size problem. but I don't see what to change ...

Hi,

In your main.cpp file, you define a private variable named custom of type QCustomPlot. Then in your mainwindow.cpp file, you write:

QCustomPlot *custom = new QCustomPlot(this);

You can not do that as the custom variable type is already defined in your header. Replace this line by this one, and it should be working

custom = new QCustomPlot(this);

At least on mine, this works perfectly. Let us know if it solves your issue.

Vincent

Thank you very much,

I've been looking for a solution, but a mistake if simple I missed it.