Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The correct way to use the “HorizontalStackedBarChart” as a clase



  • Hello,
    After testing the cited example https://doc.qt.io/qt-5.11/qtcharts-horizontalstackedbarchart-main-cpp.html , I tried to use it like a class, to freely insert into a MainWindow.
    The problem was that I couldn’t use the four last lines for create the chart:
    QMainWindow window;
    window.setCentralWidget(chartView);
    window.resize(420, 300);
    window.show();

    So I returned a pointer from chartView in mybars.cpp through QChartView *myBars::returnChart()
    mybars.cpp: https://pastebin.com/K4EkVz5R
    And in mainWindow.cpp I did setCentralWidget(bars->returnChart());

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setFixedSize(1024,768);
        bars = new myBars(this);
        bars->show();
        setCentralWidget(bars->returnChart());
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    That works, but, I wonder me, Is that the right method to use it as clase? Is there a better method to insert the chart into main window?

    Thanks in advance


Log in to reply