Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved adding points to a QLineChart from class method

    General and Desktop
    3
    5
    217
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      TimNorton last edited by TimNorton

      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);
      
      }
      
      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @TimNorton last edited by JonB

        @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.

        T 1 Reply Last reply Reply Quote 1
        • T
          TimNorton @JonB last edited by

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

          1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            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.

            T 1 Reply Last reply Reply Quote 2
            • T
              TimNorton @mrjj last edited by

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

              1 Reply Last reply Reply Quote 1
              • First post
                Last post