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

adding points to a QLineChart from class method



  • I am trying to create a QLineChart class that has a function that can add new points to the graph. The points appended in the class constructor do show up, but anything added by the method addNewPoints does not show up. How do I add graph points with a class method? I read that calling append should refresh the graph automatically, not sure how to make them show up.

    chart1.h

    #ifndef CHART_H
    #define CHART_H
    
    #include <QWidget>
    #include <QWindow>
    #include <QGridLayout>
    #include <QtCharts/QChartView>
    #include <QtCharts/QLineSeries>
    #include <QValueAxis>
    
    #include <vector>
    
    QT_CHARTS_USE_NAMESPACE
    
    class chart1 : public QWidget
    {
            Q_OBJECT
    
    public:
        explicit chart1();
        ~chart1();
    
        void addNewPoints();
    
    private:
        QLineSeries *ls1;
    
        QChart *chart;
        QChartView *chartView;
    
        QGridLayout *grid;
    
        QValueAxis *axisX;
        QValueAxis *axisY;
    
    };
    
    #include "chart1.h"
    
    chart1::chart1()
    {
        ls1 = new QLineSeries();
    
        chart = new QChart();
        chart->setTitle("Temperature");
    
        chart->addSeries(ls1);
    
        axisX = new QValueAxis;
        axisY = new QValueAxis;
    
        axisX->setRange(0,50);
        axisX->setTickCount(1);
    
        axisY->setRange(0,35);
        axisY->setTickCount(1);
    
        chart->addAxis(axisX, Qt::AlignBottom);
        chart->addAxis(axisY, Qt::AlignLeft);
    
        chart->setTheme(QChart::ChartThemeDark);
    
        chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
    
        grid = new QGridLayout;
        grid->addWidget(chartView, 0, 0);
    
        this->setLayout(grid);
    
        this->resize(800,600);
    
    }
    
    chart1::~chart1()
    {
    
    }
    
    void chart1::addNewPoints()
    {
        ls1->append(9,12);
    }
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        m_ui(new Ui::MainWindow)
    {
    
    ...
    
        graph = new chart1;
    
        graph->show();
    
        graph->addNewPoints(graph_points);
    
    }
    


  • @TimNorton

    class chart1 : public QWidget
    {
            Q_OBJECT
    
    public:
    
        // ...
    
        QLineSeries *ls1;
    
        ls1->append(1,2);
        ls1->append(3,4);
    

    Are you really allowed to do those ls1->append() statements in C++ in the public declarations section of a class in a header file? I am not a C++ expert, but that astounds me.



  • @JonB sorry, I made a mistake in copying the code. Those calls aren't actually in the header file.


  • Lifetime Qt Champion

    Hi
    Can you try

    chart1::chart1()
    {
    chart = new QChart();
    ls1 = new QLineSeries(chart); // note
    chart->setTitle("Temperature");
    ...same..

    I saw a post wher they said this helped.



  • @mrjj this worked for me, thank you very much.


Log in to reply