一、在哪儿下载?
进入官方网站:https://www.qcustomplot.com/index.php/download,根据需要选择需要的文件:
- 完整版。QCustomPlot.tar.gz 源代码+例子+帮助文档;
- 共享库。QCustomPlot-sharedlib.tar.gz 库编译和使用;
- 源代码。QCustomPlot-source.tar.gz 源代码
假如你是第一次使用,建议选择完整版。下面以完整版为例说明如何使用。
二、完整版内容解压后找到文件夹qcustomplot,内容如下:
└── qcustomplot ├── changelog.txt ├── documentation │ ├── html │ └── qcustomplot.qch ├── examples │ ├── axis-tags │ ├── interactions │ ├── plots │ ├── scrollbar-axis-range-control │ └── text-document-integration ├── GPL.txt ├── qcustomplot.cpp └── qcustomplot.h
- 更新日志 changelog.txt
- 帮助文档 documentation,安装之后可以直接查阅用法
- 例子工程 examples
- 版权声明 GPL.txt
- 源文件和头文件 qcustomplot.cpp qcustomplot.h
examples中有五个工程,分别是:
- axis-tags 轴标签
- interactions 交互
- plots 绘制
- scrollbar-axis-range-control 滚动条
- text-document-integration 输出图像到文档
以plots为例,说明demo如何利用QCustomPlot类运行和编写。找到.pro文件,用creator打开这个pro文件: pro文件内容如下:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
TARGET = plot-examples
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
../../qcustomplot.cpp
HEADERS += mainwindow.h \
../../qcustomplot.h
FORMS += mainwindow.ui
为了使用QCustomPlot这个类,项目上需要做以下两点:
- 添加了头文件和源文件的相对路径,在引用处
#include "../../qcustomplot.h"
即可。对于QT5你可能需要额外加上QT += widgets printsupport
。 - ui界面上处理,将QWidget提升为QCustomPlot,自己工程可能需要在原来的UI上添加一个QWidget,然后右键弹出的菜单选择promote to,填写好头文件和类名字,点击提升完成QCustomPlot在desinger上的表现(就好像你拖了一个QPushButtom空间一样)。
PS:假如一个类基类是QWidget,那么你可以在UI界面上直观的操作它,方法是提升。
ui->setupUi(this);
setGeometry(400, 250, 542, 390);
setupDemo(0);
//setupPlayground(ui->customPlot);
// 0: setupQuadraticDemo(ui->customPlot);
// 1: setupSimpleDemo(ui->customPlot);
// 2: setupSincScatterDemo(ui->customPlot);
// 3: setupScatterStyleDemo(ui->customPlot);
// 4: setupScatterPixmapDemo(ui->customPlot);
// 5: setupLineStyleDemo(ui->customPlot);
// 6: setupDateDemo(ui->customPlot);
// 7: setupTextureBrushDemo(ui->customPlot);
// 8: setupMultiAxisDemo(ui->customPlot);
// 9: setupLogarithmicDemo(ui->customPlot);
// 10: setupRealtimeDataDemo(ui->customPlot);
// 11: setupParametricCurveDemo(ui->customPlot);
// 12: setupBarChartDemo(ui->customPlot);
// 13: setupStatisticalDemo(ui->customPlot);
// 14: setupSimpleItemDemo(ui->customPlot);
// 15: setupItemDemo(ui->customPlot);
// 16: setupStyledDemo(ui->customPlot);
// 17: setupAdvancedAxesDemo(ui->customPlot);
// 18: setupColorMapDemo(ui->customPlot);
// 19: setupFinancialDemo(ui->customPlot);
// 20: setupPolarPlotDemo(ui->customPlot);
// for making screenshots of the current demo or all demos (for website screenshots):
//QTimer::singleShot(1500, this, SLOT(allScreenShots()));
//QTimer::singleShot(4000, this, SLOT(screenShot()));
通过setupDemo函数传入的序号,即可看到相应的Demo及其实现方法。默认是运行QuadraticDemo,运行结果如下: 将序号0改成19,运行结果如下:
仔细找找你要的绘图效果,参照源代码即可完成工程上的大多数应用。