0

I'm trying to use Qchart in ubuntu. I have a segmentation fault(sigsegv) error when running the code below. This error exists when creating the Qchart object.

header file:

#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
using namespace QtCharts;
class Test
{
  public:
    Test();
    ~Test();
    void Run();
  private:
    QLineSeries *series;
    QChart *chart;
    QChartView *chartView;
};

cpp file:

#include "test.h"
Test::Test()
{        
  series = new QLineSeries();
  chart = new QChart();
  chartView = new QChartView(chart);
}

void Test::Run(){
  series->append(0, 6);
  series->append(2, 4);
  series->append(3, 8);
  series->append(7, 4);
  series->append(10, 5);
  chart->legend();
  chart->addSeries(series);
  chart->createDefaultAxes();
  chart->setTitle("Simple line chart example");
  chartView->setRenderHint(QPainter::Antialiasing);
  chartView->show();
}
Test::~Test(){
  delete series;
  delete chart;
  delete chartView;
}

What's the problem? And how to fix this error?

  • 2
    Please provide a [mcve] that shows how you are using the code shown and also provide some information regarding the line that appears to cause the segfault. One thing to note is that when using `chartView = new QChartView(chart)`, `chartView` takes ownership of `chart`. That means `delete chart` followed by `delete chartView` will trigger a double delete and, hence, undefined behaviour. – G.M. Apr 08 '20 at 10:22
  • Did you initialize a `QGuiApplication` before running `Test::run()`? If you don't you get this segfault. If you do, you get another one, I guess some widget needs to be setup first or something.. – ypnos Apr 08 '20 at 10:59
  • @G.M. Thanks for your hint about minimal reproducible. But I don't understand your note. Could you please explain it more? – Ghasem Heydari Apr 08 '20 at 11:26
  • What G.M. is saying is that you have an error in your destructor. A `QChartView` takes ownership of its `QChart` (other than a graphics view / scene), so you should not delete the chart. However your segfault already happens in the constructor. – ypnos Apr 08 '20 at 11:31
  • @ypnos, thank you. You're right, my segfault happens in the constructor. Also, I use ' QGuiApplication' before ' Test::run()' but not solved. – Ghasem Heydari Apr 08 '20 at 11:49

1 Answers1

0

I don't know if you still need help about this but in case someone does, I will share my solution.

First of all, you need to ensure QApplication is declared and working correctly (do not confuse with QGuiApplication).

Secondly, if you necessarily need to modularize the chart (like you do by defining the Test class and define the chart attributes in it), you need to initialize the QChart (or the function or constructor that includes the initialization of QChart) in main window widget's constructor. In Windows, this approach would solve your problem:

in mainwindow.h:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
   Test testObject;
   //other stuff
}

in mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow)
{

    testObject = Test();
    //other stuff
}

Note that MainWindow is default QMainWindow widget class in Windows.

I am not sure but the source of the problem might be Q_DISABLE_COPY(QChart) line in qchart.h file. It needs to throw a compile-time error but instead causes a segmentation fault on runtime.