Qt 将带界面的程序封装成dll

摘要:
在编译之后,将不同模式编译下的dll文件放入程序编译后的文件夹中,才能正常运行程序。

1.修改配置文件

1 #TEMPLATE =app
2 
3 DEFINES +=CUSTOMMESSAGEBOX_LIBRARY
4 TEMPLATE = lib

2.在导出类的头文件上加如下代码

1 #if defined(CUSTOMMESSAGEBOX_LIBRARY)
2 #  define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_EXPORT
3 #else
4 #  define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_IMPORT
5 #endif

3.修改导出类定义

1 //class CustomMessageBox : public QDialog
2 class CUSTOMMESSAGEBOXSHARED_EXPORT CustomMessageBox : public QDialog

4.编译

若是MinGW32编译器,在编译之后会在文件夹下找到***.dll***.a文件;若是MSVC编译器,则应该是***.dll***.lib

5.使用

在使用该库的程序中,新建一个include文件夹,将***.a文件 和导出类的头文件复制进这个文件夹,在程序中引入该头文件即可。在编译之后,将不同模式编译下的dll文件放入程序编译后的文件夹中,才能正常运行程序。

6.简单案例

custommessagebox.pro

1 #-------------------------------------------------
2 #
3 # Project created by QtCreator 2020-03-07T22:37:05
4 #
5 #-------------------------------------------------
6 
7 QT       +=core gui
8 
9 greaterThan(QT_MAJOR_VERSION, 4): QT +=widgets
10 
11 TARGET =custommessagebox
12 #TEMPLATE =app
13 
14 DEFINES +=CUSTOMMESSAGEBOX_LIBRARY
15 TEMPLATE =lib
16 
17 # The following define makes your compiler emit warnings ifyou use
18 # any feature of Qt which as been marked asdeprecated (the exact warnings
19 # depend on your compiler). Please consult the documentation of the
20 # deprecated API in order to know how to port your code away fromit.
21 DEFINES +=QT_DEPRECATED_WARNINGS
22 
23 # You can also make your code fail to compile ifyou use deprecated APIs.
24 # In order to doso, uncomment the following line.
25 # You can also selectto disable deprecated APIs only up to a certain version of Qt.
26 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
27 
28 
29 SOURCES +=30 custommessagebox.cpp 
31 #        main.cpp
32 
33 HEADERS +=34 custommessagebox.h
35 
36 FORMS +=37 custommessagebox.ui
38 
39 RESOURCES +=40     resources.qrc

custommessagebox.h

1 #ifndef CUSTOMMESSAGEBOX_H
2 #define CUSTOMMESSAGEBOX_H
3 
4 #if defined(CUSTOMMESSAGEBOX_LIBRARY)
5 #  define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_EXPORT
6 #else
7 #  define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_IMPORT
8 #endif
9 
10 #include <QDialog>
11 
12 namespaceUi {
13 classCustomMessageBox;
14 }
15 
16 //class CustomMessageBox : public QDialog
17 class CUSTOMMESSAGEBOXSHARED_EXPORT CustomMessageBox : publicQDialog
18 {
19 Q_OBJECT
20 
21 public:
22     explicit CustomMessageBox(QWidget *parent = 0);
23     ~CustomMessageBox();
24 
25     void setMessage(const QString msg, inttype);
26 
27     voidtest();
28 
29 privateslots:
30     voidon_btnOk_clicked();
31 
32 private:
33     Ui::CustomMessageBox *ui;
34 
35     void initStyle(); //初始化无边框窗体
36 
37     void moveFormToCenter(); //窗体居中显示
38 };
39 
40 #endif //CUSTOMMESSAGEBOX_H

custommessagebox.cpp

1 #include "custommessagebox.h"
2 #include "ui_custommessagebox.h"
3 
4 #include <QDesktopWidget>
5 #include <QDebug>
6 
7 CustomMessageBox::CustomMessageBox(QWidget *parent) :
8 QDialog(parent),
9     ui(newUi::CustomMessageBox)
10 {
11     ui->setupUi(this);
12     this->initStyle();
13     this->moveFormToCenter();
14 }
15 
16 CustomMessageBox::~CustomMessageBox()
17 {
18     deleteui;
19 }
20 
21 void CustomMessageBox::setMessage(const QString msg, inttype)
22 {
23     switch(type) {
24         case 1:
25 {
26             ui->labIcoMain->setStyleSheet("border-image: url(:/images/image/msg_question.png);");
27             ui->lab_Title->setText("询问");
28             break;
29 }
30         case 2:
31 {
32             ui->labIcoMain->setStyleSheet("border-image: url(:/images/image/msg_error.png);");
33             ui->btnCancel->setVisible(false);
34             ui->lab_Title->setText("错误");
35             break;
36 }
37         default:
38 {
39             ui->labIcoMain->setStyleSheet("border-image: url(:/images/image/msg_info.png);");
40             ui->btnCancel->setVisible(false);
41             ui->lab_Title->setText("提示");
42             break;
43 }
44 }
45     ui->labInfo->setText(msg);
46     this->setWindowTitle(ui->lab_Title->text());
47 }
48 
49 voidCustomMessageBox::test()
50 {
51     qDebug() << "测试是否可以输出";
52 }
53 
54 voidCustomMessageBox::initStyle()
55 {
56     QFont localFont = this->font();
57     localFont.setPointSize(10);
58     localFont.setFamily("Microsoft YaHei");
59     this->setFont(localFont);
60     this->setWindowTitle(ui->lab_Title->text());
61     //设置窗体标题栏隐藏
62     this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint |Qt::WindowMinimizeButtonHint);
63     //设置图标
64     ui->lab_Ico->setStyleSheet("border-image: url(:/images/image/home.png);");
65     ui->btnMenu_Close->setIcon(this->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
66     //关联关闭按钮
67     connect(ui->btnMenu_Close, SIGNAL(clicked()), this, SLOT(close()));
68     connect(ui->btnCancel, SIGNAL(clicked()), this, SLOT(close()));
69 }
70 
71 voidCustomMessageBox::moveFormToCenter()
72 {
73     int width = this->width();
74     int height = this->height();
75 QDesktopWidget dwt;
76     int deskWidth =dwt.availableGeometry().width();
77     int deskHeight =dwt.availableGeometry().height();
78     QPoint movePoint(deskWidth / 2 - width / 2, deskHeight / 2 - height / 2);
79     this->move(movePoint);
80 }
81 
82 voidCustomMessageBox::on_btnOk_clicked()
83 {
84     done(1);
85     this->close();
86 }

custommessagebox.ui

省略

调用

1 voidWidget::on_btnMeesageBox_clicked()
2 {
3 CustomMessageBox messageBox;
4     messageBox.setMessage("调用成功", 1);
5 messageBox.exec();
6 }

免责声明:文章转载自《Qt 将带界面的程序封装成dll》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【Windows编程】系列第五篇:GDI图形绘制java 字符串转日期格式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

SqlLite 简明教程

LIMIT运算符LIMIT子句指定要返回的记录数。WHERE表达式LIMITnumberLIKE运算符LIKE操作符用于在WHERE子句的列中搜索指定的模式。SELECT可以是简单的,也可以是复合的。请注意,UNION中的SELECT语句必须具有相同数量的字段。NOT NULL约束强制字段始终包含值。SQLNULL约束NULL值缺少未知数据。SQLPRIMA...

element ui设置表格表头高度和每一行的高度

.el-table__headertr,.el-table__headerth{padding:0;height:30px;line-height:30px;}.el-table__bodytr,.el-table__bodytd{padding:0;height:30px;line-height:30px;}...

JS获取当前时间

如果有更好的方法,请提出建议。进一步解释如下:varmyDate=newDate();我的日期。getYear();//获取当前年份(2位数)myDate getFullYear();//获取完整的年份(4位数,1970-???=0)||);}//----------------------------------------------//日期格式//格式...

sqlite3 数据类型 批量插入

SQLite3采用动态数据类型。存储值的数据类型与值本身相关,而不是由其字段类型决定。SQLite3的动态数据类型可以向后兼容其他数据库常用的静态类型,这意味着在使用静态数据类型的数据库中使用的数据表也可以在SQLite3中使用。在SQLite2数据库中,除了声明为主键的INTEGER列外,任何列都可以存储属于任何存储类型的值。...

sqlserver2016安装

2008以前的版本在SQLserver配置管理其中设置。...

avue 常用修改

1.搜索栅栏调整colum中对象的属性:searchSpan:4,column:[{label:"模型名称",prop:"name",search:true,searchSpan:4,},2.搜索框字段位置长度column:[{label:"流程标题23423432",searchLabelWidth:200,3.编辑页面,字段lable宽度设置labelW...