12-3_Qt 5.9 C++开发指南_创建和使用静态链接库

第12章中的静态链接库和动态链接库介绍,都是以UI操作的方式进行,真正在实践中,可以参考UI操作产生的代码来实现同样的功能。

文章目录

  • 1. 创建静态链接库
    • 1.1 创建静态链接库过程
    • 1.2 静态链接库代码
      • 1.2.1 静态链接库可视化UI设计框架
      • 1.2.2 qwdialogpen.h
      • 1.2.3 qwdialogpen.cpp
  • 2. 静态链接库的使用
    • 2.1 静态链接库的使用过程
    • 2.2 代码
      • 2.2.1 可视化UI设计框架
      • 2.2.2 LibUser.pro
      • 2.2.3 mainwindow.h
      • 2.2.4 mainwindow.cpp

1. 创建静态链接库

1.1 创建静态链接库过程

创建一个静态链接库项目,设计各种需要导出的类,包括具有 UI 的窗体类、对话框类,编译后可以生成一个 lib 文件(MSVC 编译器生成后缀为“.lib”的文件,,MinGW编译器生成后缀为“.a”的文件),在另一个应用程序里使用这个 lib文件和类的头文件(不需要 cpp 源文件,就可以静态编译到应用程序里。

这种方式适合于在小组开发时,每个人负责自己的部分,使用其他人设计的代码时只能使用而不能看到或修改源代码,便于项目代码的管理。

创建静态链接库项目,单击 Qt Creator 的“File”->“New File or Project”菜单项,在出现的“New File or Project”对话框中选择 Projects 组里的 Library,在右侧的具体类别中再选择 C++ Library,单击“Choose…”按钮后出现如图 下图所示的向导对话框。

在这里插入图片描述

在此对话框的 Type 下拉列表框里选择“Statically Linked Library”,并给项目命名,例如myStaticLib,再选择项目保存目录。单击“Next”按钮后选择编译器,再下一步选择需要包含的Qt 模块,再下一步是类定义页面(见下图),在其中输入类的名称。

在这里插入图片描述

本实例将 9.3 节设计的一个 QPen 属性设置对话框 QWDialogPen 作为静态库的导出类,所以在上图的类定义界面上输入的类名称为 QWDialogPen,头文件和源程序文件名会自动生成。 单击“Next”按钮,再下一步结束即可。

这样生成的静态库项目 myStaticLib 包括 3个文件:myStaticLib.pro、qwdialogpen.h 和qwdialogpen.cpp。
我们希望将 9.3 节设计的一个 QPen 属性设置对话框 QWDialogPen 作为静态库的类,为此将9.3 节QWDialogPen 类相关的 3 个文件 qwdialogpen.h、qwdialogpen.cpp 和 qwdialogpen.ui 复制到 myStaticLib 项目的源文件目录下,覆盖自动生成的两个文件,并且将 qwdialogpen.ui 添加到项目中。
QWDialogPen 类相关的 3 个文件在9.3 节有详细介绍,添加到静态库项目 myStaticLib 之后无需做任何修改,所以其内容不再详述。

项目配置文件myStaticLib.pro 是对本项目的设置,其内容如下:

QT       += widgetsTARGET = myStaticLibTEMPLATE = lib
CONFIG += staticlibDEFINES += QT_DEPRECATED_WARNINGSSOURCES += \qwdialogpen.cppHEADERS += \qwdialogpen.h
unix {target.path = /usr/libINSTALLS += target
}FORMS += \qwdialogpen.ui

TEMPLATE = lib定义项目模板是库,而不是应用程序。

CONFIG += staticlib 配置项目为静态库。

TARGET = myStaticLib定义项目编译后生成的目标文件名称为myStaticLib。

注意:静态库项目可以使用 MinGW或MSVC 编译器编译,但是项目编译生成的文件与使用的编译器有关。若使用 MSVC 编译,编译后会生成一个库文件 myStaticLib.lib; 若使用 MiGW 编译,编译后会生成一个库文件libmyStaticLib.a。

release 和 debug 模式下编译生成的都是相同的文件名,并不会为 debug 版本自动添加一个字母“d”,但是在 release 和 debug 模式下编译应用程序时,需要使用相应版本的库文件。

1.2 静态链接库代码

1.2.1 静态链接库可视化UI设计框架

在这里插入图片描述

1.2.2 qwdialogpen.h

#ifndef QWDIALOGPEN_H
#define QWDIALOGPEN_H#include    <QDialog>
#include    <QPen>
//#include    "sharedlib_global.h"namespace Ui {
class QWDialogPen;
}class QWDialogPen : public QDialog
{ //QPen属性设置对话框Q_OBJECT
private:QPen    m_pen; //成员变量
public:explicit QWDialogPen(QWidget *parent = 0);~QWDialogPen();void    setPen(QPen pen); //设置QPen,用于对话框的界面显示QPen    getPen(); //获取对话框设置的QPen的属性static  QPen    getPen(QPen  iniPen, bool &ok);  //静态函数private slots:void on_btnColor_clicked();
private:Ui::QWDialogPen *ui;
};#endif // QWDIALOGPEN_H

1.2.3 qwdialogpen.cpp

#include "qwdialogpen.h"
#include "ui_qwdialogpen.h"#include    <QColorDialog>QWDialogPen::QWDialogPen(QWidget *parent) :QDialog(parent),ui(new Ui::QWDialogPen)
{ui->setupUi(this);//“线型”ComboBox的选择项设置ui->comboPenStyle->clear();ui->comboPenStyle->addItem("NoPen",0);ui->comboPenStyle->addItem("SolidLine",1);ui->comboPenStyle->addItem("DashLine",2);ui->comboPenStyle->addItem("DotLine",3);ui->comboPenStyle->addItem("DashDotLine",4);ui->comboPenStyle->addItem("DashDotDotLine",5);ui->comboPenStyle->addItem("CustomDashLine",6);ui->comboPenStyle->setCurrentIndex(1);
}QWDialogPen::~QWDialogPen()
{delete ui;
}void QWDialogPen::setPen(QPen pen)
{ //设置QPen,并刷新显示界面m_pen=pen;ui->spinWidth->setValue(pen.width()); //线宽int i=static_cast<int>(pen.style());  //枚举类型转换为整型ui->comboPenStyle->setCurrentIndex(i);QColor  color=pen.color();ui->btnColor->setAutoFillBackground(true); //设置颜色按钮的背景色QString str=QString::asprintf("background-color: rgb(%d, %d, %d);",color.red(),color.green(),color.blue());ui->btnColor->setStyleSheet(str);
}QPen QWDialogPen::getPen()
{//获得设置的属性m_pen.setStyle(Qt::PenStyle(ui->comboPenStyle->currentIndex())); //线型m_pen.setWidth(ui->spinWidth->value()); //线宽QColor  color;color=ui->btnColor->palette().color(QPalette::Button);m_pen.setColor(color); //颜色return  m_pen;
}QPen QWDialogPen::getPen(QPen iniPen,bool &ok)
{ //静态函数,获取QPenQWDialogPen *Dlg=new QWDialogPen; //创建一个对话框Dlg->setPen(iniPen); //设置初始化QPenQPen    pen;int ret=Dlg->exec(); //弹出对话框if (ret==QDialog::Accepted){pen=Dlg->getPen(); //获取ok=true;    }else{pen=iniPen;ok=false;   }delete  Dlg; //删除对话框对象return  pen; //返回设置的QPen对象
}void QWDialogPen::on_btnColor_clicked()
{//设置颜色QColor  color=QColorDialog::getColor();if (color.isValid()){ //用样式表设置QPushButton的背景色QString str=QString::asprintf("background-color: rgb(%d, %d, %d);",color.red(),color.green(),color.blue());ui->btnColor->setStyleSheet(str);}
}

2. 静态链接库的使用

2.1 静态链接库的使用过程

创建一个基于 QMainWindow 的应用程序 LibUser,在项目源程序目录下新建一个include 目录,根据使用的编译器复制不同的文件。

  • 若使用MSVC 编译器,将静态库项目 myStaticLib 下的 qwdialogpen.h 和 release 版本的myStaticLib.lib 复制到这个 include 目录下,将 debug 版本的 myStaticLib.lib 更名为myStaticLibd.lib 复制到这个 include 目录下。

  • 若使用 MinGW 编译器,就将 libmyStaticLib.a 和 libmyStaticLibd.a (debug 版本)复制到include目录里。

在这里插入图片描述

在项目管理目录树里右键单击 LibUser 项目,在快捷菜单里单击“Add Library…”菜单项,在出现的向导对话框里首先选择添加的库类型为“Extermal Library”,在向导第二步设置需要导入的静态库文件(见下图)。

在这里插入图片描述

首先选择需要导入的库文件 myStaticLib.lib,连接类型里必须选择 Static,因为这是静态库勾选Add“d”sufix for debug version,使得在 debug 模式下编译应用程序时将自动调用 debug 版本的库文件 myStaticLibd.lib。

设置完成后,Qt Creator 将自动更改项目配置文件 LibUser.pro,增加以下的内容,主要是设置了包含文件和依赖项的路径,增加了 LIBS 设置。

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLibdINCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/includewin32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLibd.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLibd.lib

编译应用程序 LibUser,使用 MSVC 或MinGW 编译器,在 release 或 debug 模式下都可以编译,运行程序效果下图 所示。单击“设置 Pe”按可以设置划线的 Pen 属性,并在主窗体上绘制一个矩形框。
在这里插入图片描述
主体程序比较简单,MainWindow类中新增的定义如下:

private:QPen    mPen;protected:void    paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;

paintEvent()事件在窗体上绘制一个矩形,使用了QPen类型的私有变量mPen作为绘图的画笔action_Pen 的响应代码调用静态库里的 QWDialogPen 的静态函数getPen 设置画笔属性。

void MainWindow::paintEvent(QPaintEvent *event)
{//绘图Q_UNUSED(event);QPainter    painter(this);QRect rect(0,0,width(),height()); //viewport矩形区painter.setViewport(rect);//设置Viewportpainter.setWindow(0,0,100,50); // 设置窗口大小,逻辑坐标painter.setRenderHint(QPainter::Antialiasing);painter.setRenderHint(QPainter::TextAntialiasing);painter.setPen(mPen);painter.drawRect(10,10,80,30);
}void MainWindow::on_action_Pen_triggered()
{//设置Penbool    ok=false;QPen    pen=QWDialogPen::getPen(mPen,ok);if (ok){mPen=pen;this->repaint();}
}

本实例将一个可视化设计的对话框 QWDialogPen 封装到一个静态库里,也可以将任何 C++类、函数封装到静态库,其实现方法是一样的。

2.2 代码

2.2.1 可视化UI设计框架

在这里插入图片描述

2.2.2 LibUser.pro

#-------------------------------------------------
#
# Project created by QtCreator 2017-04-05T00:25:31
#
#-------------------------------------------------QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = LibUser
TEMPLATE = app# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as 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# You can also make your code fail to compile if you use 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.0SOURCES += main.cpp\mainwindow.cppHEADERS  += mainwindow.hFORMS    += mainwindow.uiwin32:CONFIG(release, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/include/ -lmyStaticLibdINCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/includewin32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/libmyStaticLibd.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/include/myStaticLibd.lib

2.2.3 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>#include    <QPen>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTprivate:QPen    mPen;protected:void    paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;public:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:
//    void on_pushButton_clicked();void on_action_Pen_triggered();private:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H

2.2.4 mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"#include    "qwdialogpen.h"
#include    <QPainter>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::paintEvent(QPaintEvent *event)
{//绘图Q_UNUSED(event);QPainter    painter(this);QRect rect(0,0,width(),height()); //viewport矩形区painter.setViewport(rect);//设置Viewportpainter.setWindow(0,0,100,50); // 设置窗口大小,逻辑坐标painter.setRenderHint(QPainter::Antialiasing);painter.setRenderHint(QPainter::TextAntialiasing);painter.setPen(mPen);painter.drawRect(10,10,80,30);
}void MainWindow::on_action_Pen_triggered()
{//设置Penbool    ok=false;QPen    pen=QWDialogPen::getPen(mPen,ok);if (ok){mPen=pen;this->repaint();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/17553.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Netty3 和Netty4区别

Netty3 和Netty4区别 目录概述需求&#xff1a; 设计思路实现思路分析1.Netty3和Netty4区别2.demo 拓展实现 参考资料和推荐阅读 Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;skip hardness,make a better resul…

【华为OD机试】荒岛求生【2023 B卷|100分】

【华为OD机试】-真题 !!点这里!! 【华为OD机试】真题考点分类 !!点这里 !! 题目描述 有一个荒岛,只有左右两个港口,只有一座桥连接这两个港口,现在有一群人需要从两个港口逃生, 有的人往右逃生,有的往左逃生,如果两个人相遇,则PK,体力值大的能够打赢体力值小的, …

Java学习资料的推荐

以下是一些Java学习资料的推荐&#xff1a; 1. Java入门教程&#xff1a;https://www.runoob.com/java/java-tutorial.html 2. 阿里巴巴Java开发手册&#xff1a;https://github.com/alibaba/p3c/blob/master/README_zh_CN.md 3. Java编程思想&#xff08;Thinking in Java&…

SLA探活工具EaseProbe

工具介绍 EaseProbe可以做三种工作&#xff1a;探测、通知和报告。 项目地址&#xff1a;https://github.com/megaease/easeprobe 1、安装 [rootlocalhost ]# yum -y install unzip go [rootlocalhost ]# unzip easeprobe-main.zip [rootlocalhost ]# cd easeprobe-main [r…

数据结构--算法的时间复杂度和空间复杂度

文章目录 算法效率时间复杂度时间复杂度的概念大O的渐进表示法计算实例 时间复杂度实例 常见复杂度对比例题 算法效率 算法效率是指算法在计算机上运行时所消耗的时间和资源。这是衡量算法执行速度和资源利用情况的重要指标。 例子&#xff1a; long long Fib(int N) {if(N …

linux的内嵌汇编代码

/* C语言实现MCR指令 */ #define __STRINGIFY(x) #x #define __MCR(coproc, opcode_1, src, CRn, CRm, opcode_2) \ __ASM volatile ("MCR " __STRINGIFY(p##coproc) ", " __STRINGIFY(opcode_1) ", " \ "%0, &…

Mybatis

文章目录 MyBatis1、概述1.2、特性1.3、安装1.4、对比 2、搭建Mybatis2.1、创建一个Maven项目2.2、然后使用project structure确定java版本2.3、setting>build->build tools>maven&#xff0c;配置maven仓库、镜像、目录2.4、设置打包方式&#xff08;子模组&#xff…

MySQL的备份与还原

MySQL的备份与还原 1、MySQL的备份说明 热备&#xff1a; 在数据库正在运行下进行备份&#xff0c;备份期间&#xff0c;数据库读写均可以正常进行&#xff1b; 温备&#xff1a; 数据库可用性弱于热备&#xff0c;备份期间&#xff0c;数据库只能进行读操作&#xff0c;不…

hcip——BGP实验

要求 1.搭建toop 2.地址规划 路由器AS接口地址R11 loop0:1.1.1.1 24 loop1 : 192.168.1.1 24 g0/0/0 12.0.0.1 24 R22 64512 g0/0/0: 12.0.0.2 24 g/0/01: 172.16.0.2 19 g0/0/2: 172.16.96.2 19 R32 64512g0/0/0: 172.16.0.3 19 g0/0/1:1…

版本兼容问题——C++ 字符串库(std::basic_string::stoi)

软件开发过程中经常会存在版本更迭问题,对于如何做好版本之间的兼容性,相信也是八仙过海,各有神通。 那么,当我们确定了一个版本,怎么比较版本之间的差异呢? 且看如下代码: int CompareVersion(const std::string& strCurVersion, const std::string& strHis…

Linux systemctl命令详解

systemctl是Linux中用于管理系统服务&#xff08;例如防火墙等&#xff09;的命令行工具。它是一个功能强大的工具&#xff0c;可以启动、停止、重启、查看状态、启用或禁用系统服务&#xff0c;以及管理系统的单元&#xff08;units&#xff09;和套接字&#xff08;sockets&a…

chrome扩展控制popup页面动态切换

文章目录 1、通过控制元素的显示隐藏达到popup页面切换的效果2、通过监听页面重新加载完成不同popup的切换3、直接修改popup页面location.href&#xff0c;无需刷新页面 1、通过控制元素的显示隐藏达到popup页面切换的效果 下面在mv2版本的API下完成 实际上通过控制页面元素实…

【密码学】五、序列密码

序列密码 1、概述1.1序列密码的分类1.1.1同步序列密码1.1.2自同步序列密码 2、序列密码的组成2.1密钥序列生成器KG2.2有限状态自动机 3、LFSR 1、概述 采用一个短的种子密钥来控制某种算法获得长的密钥序列的办法&#xff0c;用以提供加解密&#xff0c;这个种子密钥的长度较短…

Qt 5. QSerialPort串口收发

1. 代码 //ex2.cpp #include "ex2.h" #include "ui_ex2.h" #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo>int static cnt 0;Ex2::Ex2(QWidget *parent): QDialog(parent), ui(new Ui::Ex2) {ui->setupUi…

写Acknowledgement的时候,latex日志出现警告

用latex写论文的时候&#xff0c;\section{Conclusion}下面添加 \backmatter \bmhead{Acknowledgments}时报错&#xff1a;错误log&#xff1a; \bmhead Package hyperref Warning: Difference (4) between bookmark levels is greater than one, level....错误原因&#xff…

【深度学习】InST,Inversion-Based Style Transfer with Diffusion Models,论文,风格迁移,实战

代码&#xff1a;https://github.com/zyxElsa/InST 论文&#xff1a;https://arxiv.org/abs/2211.13203 文章目录 AbstractIntroductionRelated WorkImage style transferText-to-image synthesisInversion of diffusion models MethodOverview ExperimentsComparison with Sty…

Java版企业电子招标采购系统源代码Spring Boot + 二次开发 + 前后端分离 构建企业电子招采平台之立项流程图

项目说明 随着公司的快速发展&#xff0c;企业人员和经营规模不断壮大&#xff0c;公司对内部招采管理的提升提出了更高的要求。在企业里建立一个公平、公开、公正的采购环境&#xff0c;最大限度控制采购成本至关重要。符合国家电子招投标法律法规及相关规范&#xff0c;以及审…

android 如何分析应用的内存(十四)——jdb命令行

android 如何分析应用的内存&#xff08;十四&#xff09; 前面的系列文章介绍了android应用如何分析native内存。 接下来就是android应用如何分析java内存。同native一样&#xff0c;我们也希望能够看到 ART的堆和栈的情况&#xff0c;以及锁的情况&#xff0c;方法的本地变…

【驱动开发day8作业】

作业1&#xff1a; 应用层代码 #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <sys/ioctl.h>int main(int…

代码随想录算法训练营day51

文章目录 Day51 最佳买卖股票时机含冷冻期题目思路代码 买卖股票的最佳时机含手续费题目思路代码 Day51 最佳买卖股票时机含冷冻期 309. 买卖股票的最佳时机含冷冻期 - 力扣&#xff08;LeetCode&#xff09; 题目 给定一个整数数组&#xff0c;其中第 i 个元素代表了第 i 天…