Qt之QCustomplot画图类
概述:
一、简介
1.QCustomPlot是Qt的一个小型第三方图表库,支持静态/动态曲线、柱状图、蜡烛图、频谱图等。使用方便,仅需在项目中加入头文件qcustomplot.h和qcustomplot.cpp源文件即可,或者把它当做一个库来添加到项目当中,需要在pro文件加入 QT += widgets printsupport
二、简单介绍QCustomPlot的几个重要类
QCustomPlot 图表类:用于图表的显示和交互
QCPLayer 图层:管理图层元素(QCPLayerable),所有可显示的对象都是继承自图层元素
QCPAbstractPlottable 绘图元素:包含 折线图(QCPGraph)、曲线图(QCPCurve)、柱状图(QCPBars)、QCPStatiBox(盒子图)、QCPColorMap(色谱图)、QCPFinancial(金融图)
QCPAxisRect 坐标轴矩形:一个坐标轴矩形默认包含上下左右四个坐标轴,但是可以添加多个坐标轴
三、简单介绍QCustomPlot类
QCustomPlot类管理着所有的图层,它默认自带了六个图层,分别是:
背景层background
网格层grid
绘图层main
坐标轴层axes
图例层legend
overlay层overlay
依据层的顺序的不同,绘制的顺序也不同,越在底下的层越早绘制,当前层默认为绘图层main
而我们的绘图区域则在QCPAxisRect中,QCustomPlot类默认包含一个QCPAxisRect,我们可以在下图中可以看到一个QCPAxisRect一般来说会有上轴xAxis2、下轴xAxis、左轴yAxis和右轴yAxis2四个轴(下面图片会有展示);
环境部署:
1.资源下载:这里就不给大家链接了直接上QQ群号:1136606088
2.使用时我们只需将源文件和都文件拷贝到工程文件夹中即可(参考下方图片);
3.在UI设计师中拖个Widget然后右键提升控件(参考下方图片);
代码示例:
.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
Widget.cpp \
qcustomplot.cpp
HEADERS += \
Widget.h \
qcustomplot.h
FORMS += \
Widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "qcustomplot.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
/**
* @brief initChart 初始化图形
*/
void initChart();
/**
* @brief setDemoData 设置图形数据
*/
void setDemoData(QCustomPlot *pCustomPlot);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
.cpp
#include "Widget.h"
#include "ui_Widget.h"
#include <QVector>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//初始化图形
initChart();
}
Widget::~Widget()
{
delete ui;
}
void Widget::initChart()
{
setDemoData(ui->customPlot);
}
void Widget::setDemoData(QCustomPlot *pCustomPlot)
{
QVector<double> x(101), y(101);
for (int i=0; i<101; ++i)
{
x[i] = i/50.0 - 1;
y[i] = x[i]*x[i];
}
//添加图形
pCustomPlot->addGraph();
//设置显示线介绍默认在右上角
pCustomPlot->legend->setVisible(true);
//设置坐标轴名称
pCustomPlot->xAxis->setLabel("x下轴");
pCustomPlot->xAxis2->setLabel("x上轴");
pCustomPlot->yAxis->setLabel("y左轴");
pCustomPlot->yAxis2->setLabel("y右轴");
//设置坐标轴文字显示隐藏
pCustomPlot->xAxis2->setVisible(false);
pCustomPlot->yAxis2->setVisible(true);
//设置坐标轴刻度是否显示
pCustomPlot->xAxis2->setTickLabels(true);
pCustomPlot->yAxis2->setTickLabels(false);
//设置坐标轴范围
pCustomPlot->xAxis->setRange(0, 1);
pCustomPlot->yAxis->setRange(0, 1);
//设置数据
pCustomPlot->graph(0)->setData(x,y);
//make left and bottom axes always transfer their ranges to right and top axes:
connect(pCustomPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), pCustomPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(pCustomPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), pCustomPlot->yAxis2, SLOT(setRange(QCPRange)));
//让范围自己缩放,所以图0完美地适合在可见区域:
pCustomPlot->rescaleAxes(/*true*/);
//允许用户用鼠标拖动轴范围,用鼠标滚轮缩放和选择图形点击:
pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}
运行效果图:
利用官方案例实现效果图:
over:
欢迎大家关注作者在文末评论、点赞、转发以及批评指正!
如果大家有更好的方法或有问题可以在文末评论一起讨论!
共同学习!
共同进步!
文末一句话:
山的价值,不在于峰峦叠嶂,而是地理位置;
水的重要,不在于积水成渊,而是包容万物;
人的品级,不在于出身阶层,而是灵魂干净。