說明
使用 QCustomPlot 繪圖庫輔助開發時整理的學習筆記,同系列文章目錄可見 《繪圖庫 QCustomPlot 學習筆記》目錄,本篇介紹 QCustomPlot 的一種使用方法,通過動態庫的方式進行使用,示例中使用的 QCustomPlot 版本為 Version 2.1.1,
- 說明
- 1. 下載原始碼
- 2. 編譯動態庫
- 2.1 編譯動態庫的工程檔案 .pro
- 2.2 整理編譯目錄
- 2.3 編譯出動態庫
- 3. 使用動態庫
- 3.1 在使用工程檔案 .pro 中添加代碼
- 3.2 使用注意事項
- 3.3 使用示例代碼
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,
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
DEFINES += QCUSTOMPLOT_COMPILE_LIBRARY
TEMPLATE = lib
CONFIG += debug_and_release build_all
static {
CONFIG += static
} else {
CONFIG += shared
}
VERSION = 2.1.1
TARGET = qcustomplot
CONFIG(debug, debug|release) {
TARGET = $$join(TARGET,,,d)
QMAKE_TARGET_PRODUCT = "QCustomPlot (debug mode)"
QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt (debug mode)"
} else {
QMAKE_TARGET_PRODUCT = "QCustomPlot"
QMAKE_TARGET_DESCRIPTION = "Plotting library for Qt"
}
QMAKE_TARGET_COMPANY = "www.qcustomplot.com"
QMAKE_TARGET_COPYRIGHT = "Copyright (C) by Emanuel Eichhammer"
SOURCES += qcustomplot.cpp
HEADERS += qcustomplot.h
2.2 整理編譯目錄
將上面的 sharedlib-compilation.pro 和 qcustomplot.h、qcustomplot.cpp 三個檔案放在同一個檔案夾下,

2.3 編譯出動態庫
使用 Qt Creator 打開 sharedlib-compilation.pro 檔案,選擇合適的編譯器,這個編譯器必須與后面使用動態庫時的編譯器一樣,比如都為 MSVC2015 64bit,(編譯時選擇 Debug 模式或者 Release 模式都可以,不影響最后的使用,因為 .pro 檔案里面有設定,不管是哪種模式,最后兩種版本都會生成,)

點擊左下角這個錘子圖示,編譯動態庫,等待編譯,

編譯完成后,會在構建目錄下生成動態庫,我的構建目錄為(因人而異):
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 檔案同級目錄下):
greaterThan(QT_MAJOR_VERSION, 4): QT += printsupport
greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
# Tell the qcustomplot header that it will be used as library:
DEFINES += QCUSTOMPLOT_USE_LIBRARY
# Link with debug version of qcustomplot if compiling in debug mode, else with release library:
CONFIG(debug, release|debug) {
win32:QCPLIB = qcustomplotd2
else: QCPLIB = qcustomplotd
} else {
win32:QCPLIB = qcustomplot2
else: QCPLIB = qcustomplot
}
LIBS += -L$$PWD -l$$QCPLIB
若使用 MinGW 編譯器,生成的靜態庫檔案名字前面可能多了 lib 三個字母,包含時需對名字做對應修改:
# Link with debug version of qcustomplot if compiling in debug mode, else with release library:
CONFIG(debug, release|debug) {
win32:QCPLIB = libqcustomplotd2
else: QCPLIB = libqcustomplotd
} else {
win32:QCPLIB = libqcustomplot2
else: QCPLIB = libqcustomplot
}
LIBS += -L$$PWD -l$$QCPLIB
添加以上代碼后,就可以按正常方式使用 QCustomPlot 繪圖庫了,
3.2 使用注意事項
通過動態庫的方式進行使用時,需注意以下幾點:
- 編譯動態庫時的編譯器版本必須和使用動態庫時的編譯器版本保持一致,
- 生成的動態庫檔案、靜態庫檔案、
qcustomplot.h檔案必須放在編譯器能找到的地方,比如.pro檔案所在目錄、生成目錄, - 不能使用
HEADERS +=的方式在.pro檔案中包含qcustomplot.h,只能通過#include的方式在相關檔案中包含該頭檔案,
3.3 使用示例代碼
工程檔案(sharedlib-usage.pro)代碼如下,其中的庫由 MSVC2015 64bit 編譯器生成:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
TARGET = sharedlib-usage
TEMPLATE = app
greaterThan(QT_MAJOR_VERSION, 4): CONFIG += c++11
lessThan(QT_MAJOR_VERSION, 5): QMAKE_CXXFLAGS += -std=c++11
# Tell the qcustomplot header that it will be used as library:
DEFINES += QCUSTOMPLOT_USE_LIBRARY
# Link with debug version of qcustomplot if compiling in debug mode, else with release library:
CONFIG(debug, release|debug) {
win32:QCPLIB = qcustomplotd2
else: QCPLIB = qcustomplotd
} else {
win32:QCPLIB = qcustomplot2
else: QCPLIB = qcustomplot
}
LIBS += -L$$PWD -l$$QCPLIB
SOURCES += \
main.cpp
主函式檔案(main.cpp)代碼如下:
#include <QApplication>
#include <QMainWindow>
#include "qcustomplot.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow window;
// setup customPlot as central widget of window:
QCustomPlot customPlot;
window.setCentralWidget(&customPlot);
// create plot (from quadratic plot example):
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];
}
customPlot.addGraph();
customPlot.graph(0)->setData(x, y);
customPlot.xAxis->setLabel("x");
customPlot.yAxis->setLabel("y");
customPlot.rescaleAxes();
window.setGeometry(100, 100, 500, 400);
window.show();
return a.exec();
}
工程目錄結構如下:

本文作者:木三百川
本文鏈接:https://www.cnblogs.com/young520/p/17490200.html
著作權宣告:本文系博主原創文章,著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請附上出處鏈接,遵循 署名-非商業性使用-相同方式共享 4.0 國際版 (CC BY-NC-SA 4.0) 著作權協議,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/555532.html
標籤:其他
上一篇:深入理解Go語言介面
下一篇:返回列表
