Voici un petit demo.
Capture01.PNG
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QChart>>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QtCharts::QChart chart1;
QtCharts::QChart chart2;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBarSet>
#include <QBarSeries>
#include <QChartView>
#include <QLineSeries>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto* barset1 = new QtCharts::QBarSet("Bar1", this);
barset1->append({4, 5, 1, 8, 7, 2, 3, 6, 9});
auto* barseries1 = new QtCharts::QBarSeries(this);
barseries1->append(barset1);
chart1.addSeries(barseries1);
auto* chartview1 = new QtCharts::QChartView(&chart1, this);
ui->verticalLayout->addWidget(chartview1);
auto* barset2 = new QtCharts::QBarSet("Bar2", this);
barset2->append({7, 2, 3, 5});
auto* barseries2 = new QtCharts::QBarSeries(this);
barseries2->append(barset2);
chart2.addSeries(barseries2);
auto* chartview2 = new QtCharts::QChartView(&chart2, this);
ui->verticalLayout_2->addWidget(chartview2);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout"/>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>