Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. adding points to a QLineChart from class method

adding points to a QLineChart from class method

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 572 Views
  • 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 Offline
    T Offline
    TimNorton
    wrote on last edited by TimNorton
    #1

    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);
    
    }
    
    JonBJ 1 Reply Last reply
    0
    • T 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);
      
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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
      1
      • JonBJ 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 Offline
        T Offline
        TimNorton
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          2
          • mrjjM mrjj

            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 Offline
            T Offline
            TimNorton
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved