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. QChart in QGridLabel
Forum Updated to NodeBB v4.3 + New Features

QChart in QGridLabel

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 530 Views 2 Watching
  • 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on last edited by Creatorczyk
    #1

    Hi,
    I would like to display three charts in a grid. For this purpose, I created the ChartManager class, but when I add a widget, the chart is not visible. Displaying the ChartManager is done throught the widget promotions. When I add a button, for example, it was visible, I checked.

    How could I fix the code to make the chart visible?

    File ChartManager.h

    #ifndef CHARTMANAGER_H
    #define CHARTMANAGER_H
    
    #include <QGridLayout>
    #include <QPushButton>
    #include <QChartView>
    
    #include "chart.h"
    
    class ChartManager : public QGridLayout
    {
    public:
        ChartManager(QWidget *parent = nullptr);
    };
    
    #endif // CHARTMANAGER_H
    

    File Chart Manager.cpp

    #include "chartmanager.h"
    
    ChartManager::ChartManager(QWidget *parent) : QGridLayout(parent)
    {
        Chart *tempChart = new Chart();
        tempChart->setTitle("Temperature Chart");
        tempChart->legend()->hide();
        tempChart->setAnimationOptions(QChart::AllAnimations);
        QChartView tempChartView(tempChart);
        tempChartView.setRenderHint(QPainter::Antialiasing);
        addWidget(&tempChartView,0,0);
    }
    

    My charts are from example "dynamic spline"

    Chart.h

    #ifndef CHART_H
    #define CHART_H
    
    #include <QtCharts/QChart>
    #include <QtCore/QTimer>
    
    QT_CHARTS_BEGIN_NAMESPACE
    class QSplineSeries;
    class QValueAxis;
    QT_CHARTS_END_NAMESPACE
    
    QT_CHARTS_USE_NAMESPACE
    
    class Chart: public QChart
    {
        Q_OBJECT
    public:
        Chart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = nullptr);
        virtual ~Chart();
    
    public slots:
        void handleTimeout();
    
    private:
        QTimer m_timer;
        QSplineSeries *m_series;
        QStringList m_titles;
        QValueAxis *m_axisX;
        QValueAxis *m_axisY;
        qreal m_step;
        qreal m_x;
        qreal m_y;
    };
    
    #endif /* CHART_H */
    

    Chart.cpp

    #include "chart.h"
    #include <QtCharts/QAbstractAxis>
    #include <QtCharts/QSplineSeries>
    #include <QtCharts/QValueAxis>
    #include <QtCore/QRandomGenerator>
    #include <QtCore/QDebug>
    
    Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
        QChart(QChart::ChartTypeCartesian, parent, wFlags),
        m_series(nullptr),
        m_axisX(new QValueAxis()),
        m_axisY(new QValueAxis()),
        m_step(0),
        m_x(5),
        m_y(1)
    {
        QObject::connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
        m_timer.setInterval(1000);
    
        m_series = new QSplineSeries(this);
        QPen green(Qt::red);
        green.setWidth(3);
        m_series->setPen(green);
        m_series->append(m_x, m_y);
    
        addSeries(m_series);
    
        addAxis(m_axisX,Qt::AlignBottom);
        addAxis(m_axisY,Qt::AlignLeft);
        m_series->attachAxis(m_axisX);
        m_series->attachAxis(m_axisY);
        m_axisX->setTickCount(5);
        m_axisX->setRange(0, 10);
        m_axisY->setRange(-5, 10);
    
        m_timer.start();
    }
    
    Chart::~Chart()
    {
    
    }
    
    void Chart::handleTimeout()
    {
        qreal x = plotArea().width() / m_axisX->tickCount();
        qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
        m_x += y;
        m_y = QRandomGenerator::global()->bounded(5) - 2.5;
        m_series->append(m_x, m_y);
        scroll(x, 0);
        if (m_x == 100.0)
            m_timer.stop();
    }
    
    Pl45m4P 1 Reply Last reply
    0
    • C Creatorczyk

      Hi,
      I would like to display three charts in a grid. For this purpose, I created the ChartManager class, but when I add a widget, the chart is not visible. Displaying the ChartManager is done throught the widget promotions. When I add a button, for example, it was visible, I checked.

      How could I fix the code to make the chart visible?

      File ChartManager.h

      #ifndef CHARTMANAGER_H
      #define CHARTMANAGER_H
      
      #include <QGridLayout>
      #include <QPushButton>
      #include <QChartView>
      
      #include "chart.h"
      
      class ChartManager : public QGridLayout
      {
      public:
          ChartManager(QWidget *parent = nullptr);
      };
      
      #endif // CHARTMANAGER_H
      

      File Chart Manager.cpp

      #include "chartmanager.h"
      
      ChartManager::ChartManager(QWidget *parent) : QGridLayout(parent)
      {
          Chart *tempChart = new Chart();
          tempChart->setTitle("Temperature Chart");
          tempChart->legend()->hide();
          tempChart->setAnimationOptions(QChart::AllAnimations);
          QChartView tempChartView(tempChart);
          tempChartView.setRenderHint(QPainter::Antialiasing);
          addWidget(&tempChartView,0,0);
      }
      

      My charts are from example "dynamic spline"

      Chart.h

      #ifndef CHART_H
      #define CHART_H
      
      #include <QtCharts/QChart>
      #include <QtCore/QTimer>
      
      QT_CHARTS_BEGIN_NAMESPACE
      class QSplineSeries;
      class QValueAxis;
      QT_CHARTS_END_NAMESPACE
      
      QT_CHARTS_USE_NAMESPACE
      
      class Chart: public QChart
      {
          Q_OBJECT
      public:
          Chart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = nullptr);
          virtual ~Chart();
      
      public slots:
          void handleTimeout();
      
      private:
          QTimer m_timer;
          QSplineSeries *m_series;
          QStringList m_titles;
          QValueAxis *m_axisX;
          QValueAxis *m_axisY;
          qreal m_step;
          qreal m_x;
          qreal m_y;
      };
      
      #endif /* CHART_H */
      

      Chart.cpp

      #include "chart.h"
      #include <QtCharts/QAbstractAxis>
      #include <QtCharts/QSplineSeries>
      #include <QtCharts/QValueAxis>
      #include <QtCore/QRandomGenerator>
      #include <QtCore/QDebug>
      
      Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
          QChart(QChart::ChartTypeCartesian, parent, wFlags),
          m_series(nullptr),
          m_axisX(new QValueAxis()),
          m_axisY(new QValueAxis()),
          m_step(0),
          m_x(5),
          m_y(1)
      {
          QObject::connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
          m_timer.setInterval(1000);
      
          m_series = new QSplineSeries(this);
          QPen green(Qt::red);
          green.setWidth(3);
          m_series->setPen(green);
          m_series->append(m_x, m_y);
      
          addSeries(m_series);
      
          addAxis(m_axisX,Qt::AlignBottom);
          addAxis(m_axisY,Qt::AlignLeft);
          m_series->attachAxis(m_axisX);
          m_series->attachAxis(m_axisY);
          m_axisX->setTickCount(5);
          m_axisX->setRange(0, 10);
          m_axisY->setRange(-5, 10);
      
          m_timer.start();
      }
      
      Chart::~Chart()
      {
      
      }
      
      void Chart::handleTimeout()
      {
          qreal x = plotArea().width() / m_axisX->tickCount();
          qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
          m_x += y;
          m_y = QRandomGenerator::global()->bounded(5) - 2.5;
          m_series->append(m_x, m_y);
          scroll(x, 0);
          if (m_x == 100.0)
              m_timer.stop();
      }
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Creatorczyk

      Why you subclassed QGridLayout for your ChartManager?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      C 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @Creatorczyk

        Why you subclassed QGridLayout for your ChartManager?

        C Offline
        C Offline
        Creatorczyk
        wrote on last edited by
        #3

        @Pl45m4 Because I want create my own widget based on QGridLayout but showing charts

        Pl45m4P 1 Reply Last reply
        0
        • C Creatorczyk

          @Pl45m4 Because I want create my own widget based on QGridLayout but showing charts

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Creatorczyk said in QChart in QGridLabel:

          my own widget

          Subclassing QWidget would work better in your case. Make your ChartManager a QWidget, then assign a layout with your Charts


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

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

            Hi
            You also have a Widget that will run out of scope and get deleted

            #include "chartmanager.h"
            
            ChartManager::ChartManager(QWidget *parent) : QGridLayout(parent)
            {
                Chart *tempChart = new Chart();
                tempChart->setTitle("Temperature Chart");
                tempChart->legend()->hide();
                tempChart->setAnimationOptions(QChart::AllAnimations);
            
                QChartView tempChartView(tempChart); <<<<< this local vaiable and will be deleted at end of ctor
            
                tempChartView.setRenderHint(QPainter::Antialiasing);
                addWidget(&tempChartView,0,0);
            }
            

            So you should do
            QChartView * tempChartView = new QChartView
            to avoid this.

            C 1 Reply Last reply
            2
            • mrjjM mrjj

              Hi
              You also have a Widget that will run out of scope and get deleted

              #include "chartmanager.h"
              
              ChartManager::ChartManager(QWidget *parent) : QGridLayout(parent)
              {
                  Chart *tempChart = new Chart();
                  tempChart->setTitle("Temperature Chart");
                  tempChart->legend()->hide();
                  tempChart->setAnimationOptions(QChart::AllAnimations);
              
                  QChartView tempChartView(tempChart); <<<<< this local vaiable and will be deleted at end of ctor
              
                  tempChartView.setRenderHint(QPainter::Antialiasing);
                  addWidget(&tempChartView,0,0);
              }
              

              So you should do
              QChartView * tempChartView = new QChartView
              to avoid this.

              C Offline
              C Offline
              Creatorczyk
              wrote on last edited by
              #6

              @mrjj Thanks, it's works

              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