Vito Vito - 2 months ago 70
C++ Question

Why Qt chart example does not delete dynamically allocated variables

I have been checking the Qt BarChart example, I noticed that the dynamically allocated variables were not deleted at all, why? I try to copy the idea of this example, then use in a layout, and display the chart in a plainTextEditor. However, I want to delete these dynamically allocated variables once I display the chart, but I don't know when and where to delete them. I tried to delete them at the end of the function, but it won't show anything on the plainTextEditor. I don't want to define these dynamically allocated variables as a member variables and delete them in the Destructor.

Here are the example I get from the Qt Documentation

> #include <QtWidgets/QApplication>
> #include <QtWidgets/QMainWindow>
> #include <QtCharts/QChartView>
> #include <QtCharts/QBarSeries>
> #include <QtCharts/QBarSet>
> #include <QtCharts/QLegend>
> #include <QtCharts/QBarCategoryAxis>
>
> QT_CHARTS_USE_NAMESPACE
>
> int main(int argc, char *argv[]) {
> QApplication a(argc, argv);
>
> QBarSet *set0 = new QBarSet("Jane");
> QBarSet *set1 = new QBarSet("John");
> QBarSet *set2 = new QBarSet("Axel");
> QBarSet *set3 = new QBarSet("Mary");
> QBarSet *set4 = new QBarSet("Samantha");
>
> *set0 << 1 << 2 << 3 << 4 << 5 << 6;
> *set1 << 5 << 0 << 0 << 4 << 0 << 7;
> *set2 << 3 << 5 << 8 << 13 << 8 << 5;
> *set3 << 5 << 6 << 7 << 3 << 4 << 5;
> *set4 << 9 << 7 << 5 << 3 << 1 << 2;
>
> QBarSeries *series = new QBarSeries();
> series->append(set0);
> series->append(set1);
> series->append(set2);
> series->append(set3);
> series->append(set4);
>
>
> QChart *chart = new QChart();
> chart->addSeries(series);
> chart->setTitle("Simple barchart example");
> chart->setAnimationOptions(QChart::SeriesAnimations);
>
> QStringList categories;
> categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
> QBarCategoryAxis *axis = new QBarCategoryAxis();
> axis->append(categories);
> chart->createDefaultAxes();
> chart->setAxisX(axis, series);
>
> chart->legend()->setVisible(true);
> chart->legend()->setAlignment(Qt::AlignBottom);
>
> QChartView *chartView = new QChartView(chart);
> chartView->setRenderHint(QPainter::Antialiasing);
>
> QMainWindow window;
> window.setCentralWidget(chartView);
> window.resize(420, 300);
> window.show();
>
> return a.exec(); }

Answer

In Qt, certain functions are designated to take ownership, forming a parent/child relationship. And parents delete their children when they are destroyed.

With the example you've shown, series is the parent of set0 through set4 (from the calls to series->append). chart is the parent of axis and series (from chart->setAxisX). chartView is the parent of chart (from its construction: QChartView *chartView = new QChartView(chart)). And finally window is the parent of chartView (from window.setCentralWidget). And since window is not a pointer, but an actual QMainWindow object with automatic storage duration, it will be destroyed at the end of the scope, taking all of its descendants with it.