【QCustomPlot】使用方法(动态库方式)

打印 上一主题 下一主题

主题 996|帖子 996|积分 2988

说明

使用 QCustomPlot 绘图库辅助开发时整理的学习笔记。同系列文章目录可见 《绘图库 QCustomPlot 学习笔记》目录。本篇介绍 QCustomPlot 的一种使用方法,通过动态库的方式进行使用,示例中使用的 QCustomPlot 版本为 Version 2.1.1。

目录


1. 下载源码

详见本人另一篇博客 【QCustomPlot】下载,下载 QCustomPlot-sharedlib.tar.gz 动态库版的压缩包,解压后里面有个 readme.txt 文件,介绍了如何编译 QCustomPlot 动态库以及如何使用编译出来的动态库,本篇博客将以此为参考,介绍如何通过动态库的方式使用 QCustomPlot 绘图库。编译动态库时,需使用到 qcustomplot.h 与 qcustomplot.cpp 两个文件。使用动态库时,需把 qcustomplot.h 文件及动态库放在编译器能找到的地方,并在相关文件中通过 #include 的方式包含该头文件,而不能在 pro/pri 文件中通过 HEADERS += 的方式包含 qcustomplot.h ,否则会报错。
2. 编译动态库

编译动态库时,需三个文件:pro 文件、qcustomplot.h 与 qcustomplot.cpp 源码文件。
2.1 编译动态库的工程文件 .pro

pro 文件用于设定动态库的编译方式及相关信息,新建一个 txt 文本文件,将以下代码拷贝进去,然后更改 .txt 后缀名为 .pro,就得到了所需的工程文件,不妨将该工程文件命名为 sharedlib-compilation.pro。
  1. QT += core gui
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
  3. greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
  4. lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
  5. DEFINES += QCUSTOMPLOT_COMPILE_LIBRARY
  6. TEMPLATE = lib
  7. CONFIG += debug_and_release build_all
  8. static {
  9.   CONFIG += static
  10. } else {
  11.   CONFIG += shared
  12. }
  13. VERSION = 2.1.1
  14. TARGET = qcustomplot
  15. CONFIG(debug, debug|release) {
  16.   TARGET = $$join(TARGET,,,d)
  17.   QMAKE_TARGET_PRODUCT = "QCustomPlot (debug mode)"
  18.   QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt (debug mode)"
  19. } else {
  20.   QMAKE_TARGET_PRODUCT = "QCustomPlot"
  21.   QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt"
  22. }
  23. QMAKE_TARGET_COMPANY = "www.qcustomplot.com"
  24. QMAKE_TARGET_COPYRIGHT = "Copyright (C) by Emanuel Eichhammer"
  25. SOURCES += qcustomplot.cpp
  26. HEADERS += qcustomplot.h
复制代码
2.2 整理编译目录

将上面的 sharedlib-compilation.pro 和 qcustomplot.h、qcustomplot.cpp 三个文件放在同一个文件夹下。

2.3 编译出动态库

使用 Qt Creator 打开 sharedlib-compilation.pro 文件,选择合适的编译器,这个编译器必须与后面使用动态库时的编译器一样,比如都为 MSVC2015 64bit。(编译时选择 Debug 模式或者 Release 模式都可以,不影响最后的使用,因为 .pro 文件里面有设置,不管是哪种模式,最后两种版本都会生成。)

点击左下角这个锤子图标,编译动态库,等待编译。

编译完成后,会在构建目录下生成动态库,我的构建目录为(因人而异):
  1. E:\Cworkspace\Qt 5.9\QtDemo\build-sharedlib-compilation-Desktop_Qt_5_9_2_MSVC2015_64bit-Debug
复制代码
该目录的 debug 与 release 子目录下分别有对应版本的动态库,使用时只需要 .lib 以及 .dll 文件(不同平台编译器的生成结果会有差异)。


3. 使用动态库

使用动态库时,需把以下三个文件放在编译器能找到的地方:上一步生成的 .lib 以及 .dll 文件(不同平台编译器的生成结果会有差异,但都是一个静态库文件和一个动态库文件)、qcustomplot.h 文件。同样以 MSVC2015 64bit 为例。
3.1 在使用工程文件 .pro 中添加代码

在使用动态库的 .pro 工程文件中添加以下代码(库的路径因人而异,下面假设动态库放在了 .pro 文件同级目录下):
  1. greaterThan(QT_MAJOR_VERSION, 4): QT += printsupport
  2. greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
  3. lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
  4.    
  5. # Tell the qcustomplot header that it will be used as library:
  6. DEFINES += QCUSTOMPLOT_USE_LIBRARY
  7. # Link with debug version of qcustomplot if compiling in debug mode, else with release library:
  8. CONFIG(debug, release|debug) {
  9.   win32:QCPLIB = qcustomplotd2
  10.   else: QCPLIB = qcustomplotd
  11. } else {
  12.   win32:QCPLIB = qcustomplot2
  13.   else: QCPLIB = qcustomplot
  14. }
  15. LIBS += -L$$PWD -l$$QCPLIB
复制代码
若使用 MinGW 编译器,生成的静态库文件名字前面可能多了 lib 三个字母,包含时需对名字做对应修改:
  1. # Link with debug version of qcustomplot if compiling in debug mode, else with release library:
  2. CONFIG(debug, release|debug) {
  3.   win32:QCPLIB = libqcustomplotd2
  4.   else: QCPLIB = libqcustomplotd
  5. } else {
  6.   win32:QCPLIB = libqcustomplot2
  7.   else: QCPLIB = libqcustomplot
  8. }
  9. LIBS += -L$$PWD -l$$QCPLIB
复制代码
添加以上代码后,就可以按正常方式使用 QCustomPlot 绘图库了。
3.2 使用注意事项

通过动态库的方式进行使用时,需注意以下几点:

  • 编译动态库时的编译器版本必须和使用动态库时的编译器版本保持一致。
  • 生成的动态库文件、静态库文件、qcustomplot.h 文件必须放在编译器能找到的地方,比如 .pro 文件所在目录、生成目录。
  • 不能使用 HEADERS += 的方式在 .pro 文件中包含 qcustomplot.h,只能通过 #include 的方式在相关文件中包含该头文件。
3.3 使用示例代码

工程文件(sharedlib-usage.pro)代码如下,其中的库由 MSVC2015 64bit 编译器生成:
  1. QT       += core gui
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
  3. TARGET = sharedlib-usage
  4. TEMPLATE = app
  5. greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
  6. lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
  7. # Tell the qcustomplot header that it will be used as library:
  8. DEFINES += QCUSTOMPLOT_USE_LIBRARY
  9. # Link with debug version of qcustomplot if compiling in debug mode, else with release library:
  10. CONFIG(debug, release|debug) {
  11.   win32:QCPLIB = qcustomplotd2
  12.   else: QCPLIB = qcustomplotd
  13. } else {
  14.   win32:QCPLIB = qcustomplot2
  15.   else: QCPLIB = qcustomplot
  16. }
  17. LIBS += -L$$PWD -l$$QCPLIB
  18. SOURCES += \
  19.         main.cpp
复制代码
主函数文件(main.cpp)代码如下:
  1. #include <QApplication>
  2. #include <QMainWindow>
  3. #include "qcustomplot.h"
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     QMainWindow window;
  8.     // setup customPlot as central widget of window:
  9.     QCustomPlot customPlot;
  10.     window.setCentralWidget(&customPlot);
  11.     // create plot (from quadratic plot example):
  12.     QVector<double> x(101), y(101);
  13.     for (int i=0; i<101; ++i)
  14.     {
  15.         x[i] = i/50.0 - 1;
  16.         y[i] = x[i]*x[i];
  17.     }
  18.     customPlot.addGraph();
  19.     customPlot.graph(0)->setData(x, y);
  20.     customPlot.xAxis->setLabel("x");
  21.     customPlot.yAxis->setLabel("y");
  22.     customPlot.rescaleAxes();
  23.     window.setGeometry(100, 100, 500, 400);
  24.     window.show();
  25.     return a.exec();
  26. }
复制代码
工程目录结构如下:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表