目录
一、使用官方pro工程示例进行动态库的创建
二、使用官方pro工程示例进行lib库调用
三、自己建立工程调用lib文件
首先登录QCustomPlot绘图控件的官方网址:Qt Plotting Widget QCustomPlot - Introduction,去下载共享库的工程文件、调用测试文件,以及源码文件,如下图所示的两个文件包。
下载完成后放在一个文件夹内,如LIB文件夹,并分别解压。根据文件夹内的readme文件提示,需要将源码放置在工程文件的上级的上级目录。
将另一个源码压缩包解压后的源码文件拷贝至要求的文件目录内(源码放置在工程文件的上级的上级目录)。打开工程文件的内容,可以看到默认的路径要求。
打开工程文件 sharedlib-compilation.pro,并选择MinGW编译器。
工程文件内的其它信息,DEFINES += QCUSTOMPLOT_COMPILE_LIBRARY。从官方的说明文档中也可以看见宏定义要求。
本工程文件官方已经全部设置好,现在开始编译生成.a 和 dll文件。
进入sharedlib-usage文件夹,打开sharedlib-usage工程目录,同样选择minGW编译器,
打开pro工程文件内容,可以看见宏定义为DEFINES += QCUSTOMPLOT_USE_LIBRARY,该工程是app工程非lib工程。
在工程目录新建lib文件,并将第一节生成的文件拷贝至文件夹内备用。
在工程内添加lib文件,并选择“外部库”。
现在开始编译,注意在debug模式下,因为第一节生成的dll文件为debug文件,运行效果如下。
注意的地方。
(1)在工程文件中添加宏定义,DEFINES += QCUSTOMPLOT_USE_LIBRARY
(2)在工程文件中添加 QT += printsupport
(3)添加库路径。
(4)拷贝库的头文件到工程目录,include头文件,注意路径。注意是隐式调用,不要在项目中 “添加已存在文件”的方式引入库头文件,否则会报错。 error: definition of static data member 'QCPPainter::staticMetaObject' of dllimport'd class
pro工程文件内容参考:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += 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
DEFINES += QCUSTOMPLOT_USE_LIBRARY
# 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
HEADERS += \
Widget.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
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/ -lqcustomplot2
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -lqcustomplotd2
INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.
cpp文件内容:
#include "Widget.h"
#include "ui_Widget.h"
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QCustomPlot *customPlot = ui->customPlot;
// create plot (from quadratic plot example):
QVector 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();
}
Widget::~Widget()
{
delete ui;
}
效果图如下:
调用debug 和release的不同版本,注意名字设置正确。
以上工程文件地址:
https://download.csdn.net/download/dyxcc/87209744