QChart in QGridLabel
-
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(); }
-
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(); }
Why you subclassed
QGridLayout
for yourChartManager
? -
Why you subclassed
QGridLayout
for yourChartManager
?@Pl45m4 Because I want create my own widget based on QGridLayout but showing charts
-
@Pl45m4 Because I want create my own widget based on QGridLayout but showing charts
@Creatorczyk said in QChart in QGridLabel:
my own widget
Subclassing
QWidget
would work better in your case. Make yourChartManager
aQWidget
, then assign a layout with yourCharts
-
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. -
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.@mrjj Thanks, it's works