一、QCustomPlot介绍
QCustomPlot是一个基于Qt画图和数据可视化的C++控件。在Qt下的绘图工具有Qwt、QChart和QCustomPlot,置于选择哪个绘图工具各有优缺点。
在绘制大量数据(10万个点以上)时选择QCustomPlot,在数据量比较小时,QChart和QCustomPlot相差无几。
二、 QCustomPlot的下载
QCustomPlot官网地址:https://www.qcustomplot.com/index.php/introduction
下载地址:https://www.qcustomplot.com/index.php/download
目前最新的是2.1.1,直接下载QCustomPlot.tar.gz、QCustomPlot-sharedlib.tar.gz两个文件并解压。

三、QCustomPlot的几个重要类
- QCustomPlot 图表类:用于图表的显示和交互;
- QCPLayer 图层:管理图层元素(QCPLayerable),所有可显示的对象都是继承自图层元素;
- QCPAbstractPlottable 绘图元素:包含 折线图(QCPGraph)、曲线图(QCPCurve)、柱状图(QCPBars)、QCPStatiBox(盒子图)、QCPColorMap(色谱图)、QCPFinancial(金融图);
- QCPAxisRect 坐标轴矩形:一个坐标轴矩形默认包含上下左右四个坐标轴,但是可以添加多个坐标轴;
四、QCustomPlot在纯QT的使用
4.1 使用源码
- Qt新建一个工程文件,QCustomPlot.tar.gz解压后将qcustomplot.h与qcustomplot.cpp拷贝到工程目录下,右键 -> 添加现有文件…,将这两个文件添加至工程。

- 在pro中添加(由于QCustomPlot中存在导出功能,使用了printsupport模块)
QT += printsupport
- 在使用qcustomplot的文件中添加包含头文件:
#include "qcustomplot.h"
-
在*.ui文件中,选择widget控件,右键将其提升为QCustomPlot,然后头文件哪里会自动填充为qcustomplot.h。
-
运行程序就可以看见控件效果了,由于直接使用
QCustomPlot源码,所以编译运行速度会比较慢。
4.2 使用QCustomPlot编译成动态库
解压QCustomPlot-sharedlib.tar.gz,将QCustomPlot.tar.gz中的qcustomplot.cpp和qcustomplot文件复制到和QCustomPlot-sharedlib.tar.gz解压的文件夹同级目录下,如下图:

用Qt打开项目,我的路纪C:\QCustomPlot-sharedlib\qcustomplot-sharedlib\sharedlib-compilation\sharedlib-compilation.pro

选择debug或者Release,进行构建。将会在debug目录下生成libqcustomplotd2.a、qcustomplotd2.dll,在release目录下生成libqcustomplot2.a、qcustomplot2.dll。接下来我们在使用的时候只需要我拷贝qcustomplot.h和动态库就可以。
4.3 添加帮助文档
由于下载是第三方的C++控件库,没有帮助文档。需要我们人为的手动添加。
在下载的documentation文件夹下有个qcustomplot.qch文件,将其拷贝到Qt的文档路径(C:\Qt\Qt5.13.0\Docs),在QtCreator->工具->工具->选项->帮助->文档->添加,选择qcustomplot.qch文件,确定,这样以后我们F1就可以跳转到QCustomPlot的帮助文档了。

五、QCustomPlot在VS2019使用
5.1 使用源码
1. 官网下载QCustomPlot,注意是QCustomPlot.tar.gz这个文件,里面包含源码和示例。
2. 将其中的qcstomplot.h和qcustomplot.cpp添加进项目中;
3. QCustomPlot还需要依赖printsupport模块,配置如下:
4. 在UI界面拉一个widget控件。
5. 右键选择提升为…

6. 提升的类名称输入:QCustomPlot;点击添加;最后点击提升。

7. 编译运行程序即可。
5.2 目前遇到的问题
1. 使用QCustomPlot报错:错误C4996
错误解释:在Qt框架中,error C499 ‘QPainter::HighQualityAntialiasing’: Use Antialiasing instead 这个错误表明你正在使用 QPainter::HighQualityAntialiasing 这个已经被废弃的枚举值。从Qt 5开始,HighQualityAntialiasing 被新的 Antialiasing 设置取代。
解决方法1:在报错或告警行前添加: #pragma warning(suppress : 4996)。
解决方法2:将出现的HighQualityAntialiasing替换为Antialiasing 。

3366

被折叠的 条评论
为什么被折叠?



