本例指定一系列离散点,可绘制曲线图,并可将图表嵌入QWidget中
效果如下:
本例曲线图绘制使用QT的QChart模块实现(QT5.7及其以上版本才有此功能),自行封装了Chart类,
调用如下:
#-------------------------------------------------
#
# Project created by QtCreator 2020-08-12T20:05:22
#
#-------------------------------------------------
QT += core gui
QT += charts
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QCharts_Test
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp \
chart.cpp
HEADERS += \
mainwindow.h \
chart.h
FORMS += \
mainwindow.ui
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include "chart.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
Chart *chart;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void initChart();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initChart();
}
MainWindow::~MainWindow()
{
delete ui;
}
//初始化图表
void MainWindow::initChart()
{
//设置表头
chart = new Chart(this,"历史数据");
//设置坐标系
chart->setAxis("X轴",0,100,11, "Y轴",0,20,11);
//设置离散点数据
QList<QPointF> pointlist = {
QPointF(0,1), QPointF(10,2), QPointF(20,4), QPointF(30,8), QPointF(40,16), \
QPointF(50,16), QPointF(60,8), QPointF(70,4), QPointF(80,2), QPointF(90,1),};
//绘制
chart->buildChart(pointlist);
//为MainWindow的centralWidget添加一个布局
QHBoxLayout *pHLayout = new QHBoxLayout(ui->centralWidget);
//将chart添加到布局中
pHLayout->addWidget(chart);
}
#ifndef CHART_H
#define CHART_H
#include
#include
#include
#include
#include
QT_CHARTS_USE_NAMESPACE
class Chart : public QWidget
{
Q_OBJECT
QChart *qchart;
QChartView *chartview;
QSplineSeries *series;
QHBoxLayout *layout;
QValueAxis *axisX;
QValueAxis *axisY;
QString chartname;
//坐标轴参数
QString xname;
qreal xmin;
qreal xmax;
int xtickc;
QString yname;
qreal ymin;
qreal ymax;
int ytickc;
public:
Chart(QWidget* parent = 0, QString _chartname = "曲线图");
~Chart(){
}
void setAxis(QString _xname, qreal _xmin, qreal _xmax, int _xtickc, \
QString _yname, qreal _ymin, qreal _ymax, int _ytickc);
void buildChart(QList<QPointF> pointlist);
};
#endif // CHART_H
#include "chart.h"
Chart::Chart(QWidget* parent, QString _chartname){
setParent(parent);
chartname = _chartname;
series = new QSplineSeries(this);
qchart = new QChart;
chartview = new QChartView(qchart);
layout = new QHBoxLayout(this);
axisX = new QValueAxis(this);
axisY = new QValueAxis(this);
//在ui里面添加了一个Widget并把曲线图添加进去
layout->addWidget(chartview);
setLayout(layout);
chartview->setRenderHint(QPainter::Antialiasing);//防止图形走样
}
void Chart::setAxis(QString _xname, qreal _xmin, qreal _xmax, int _xtickc, \
QString _yname, qreal _ymin, qreal _ymax, int _ytickc){
xname = _xname; xmin = _xmin; xmax = _xmax; xtickc = _xtickc;
yname = _yname; ymin = _ymin; ymax = _ymax; ytickc = _ytickc;
axisX->setRange(xmin, xmax); //设置范围
axisX->setLabelFormat("%u"); //设置刻度的格式
/************************************
%u 无符号十进制整数
%s 字符串
%c 一个字符
%d 有符号十进制整数
%e 浮点数、e-记数法
%f 浮点数、十进制记数法
%s 字符串
****************************************/
axisX->setGridLineVisible(true); //网格线可见
axisX->setTickCount(xtickc); //设置多少个大格
axisX->setMinorTickCount(1); //设置每个大格里面小刻度线的数目
axisX->setTitleText(xname); //设置描述
axisY->setRange(ymin, ymax);
axisY->setLabelFormat("%u");
axisY->setGridLineVisible(true);
axisY->setTickCount(ytickc);
axisY->setMinorTickCount(1);
axisY->setTitleText(yname);
qchart->addAxis(axisX, Qt::AlignBottom); //下:Qt::AlignBottom 上:Qt::AlignTop
qchart->addAxis(axisY, Qt::AlignLeft); //左:Qt::AlignLeft 右:Qt::AlignRight
}
void Chart::buildChart(QList<QPointF> pointlist)
{
//创建数据源
series->setPen(QPen(Qt::blue,3,Qt::SolidLine));
series->clear();
for(int i=0; i<pointlist.size();i++)
series->append(pointlist.at(i).x(), pointlist.at(i).y());
qchart->setTitle(chartname);
qchart->setAnimationOptions(QChart::SeriesAnimations);//设置曲线动画模式
qchart->legend()->hide(); //隐藏图例
qchart->addSeries(series);//输入数据
qchart->setAxisX(axisX, series);
qchart->setAxisY(axisY, series);
}