0
点赞
收藏
分享

微信扫一扫

四十四、Qt之将带界面的程序封装成dll

花明 2022-06-21 阅读 54

1.修改配置文件

#TEMPLATE = app

DEFINES += CUSTOMMESSAGEBOX_LIBRARY
TEMPLATE = lib

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

#if defined(CUSTOMMESSAGEBOX_LIBRARY)
# define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_EXPORT
#else
# define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_IMPORT
#endif

3.修改导出类定义

//class CustomMessageBox : public QDialog
class CUSTOMMESSAGEBOXSHARED_EXPORT CustomMessageBox : public QDialog

4.编译

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

5.使用

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

6.简单案例

custommessagebox.pro

#-------------------------------------------------
#
# Project created by QtCreator 2020-03-07T22:37:05
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = custommessagebox
#TEMPLATE = app

DEFINES += CUSTOMMESSAGEBOX_LIBRARY
TEMPLATE = lib

# 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.0


SOURCES += \
custommessagebox.cpp \
# main.cpp

HEADERS += \
custommessagebox.h

FORMS += \
custommessagebox.ui

RESOURCES += \
resources.qrc

custommessagebox.h

#ifndef CUSTOMMESSAGEBOX_H
#define CUSTOMMESSAGEBOX_H

#if defined(CUSTOMMESSAGEBOX_LIBRARY)
# define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_EXPORT
#else
# define CUSTOMMESSAGEBOXSHARED_EXPORT Q_DECL_IMPORT
#endif

#include <QDialog>

namespace Ui {
class CustomMessageBox;
}

//class CustomMessageBox : public QDialog
class CUSTOMMESSAGEBOXSHARED_EXPORT CustomMessageBox : public QDialog
{
Q_OBJECT

public:
explicit CustomMessageBox(QWidget *parent = 0);
~CustomMessageBox();

void setMessage(const QString msg, int type);

void test();

private slots:
void on_btnOk_clicked();

private:
Ui::CustomMessageBox *ui;

void initStyle(); //初始化无边框窗体

void moveFormToCenter(); //窗体居中显示
};

#endif // CUSTOMMESSAGEBOX_H

custommessagebox.cpp

#include "custommessagebox.h"
#include "ui_custommessagebox.h"

#include <QDesktopWidget>
#include <QDebug>

CustomMessageBox::CustomMessageBox(QWidget *parent) :
QDialog(parent),
ui(new Ui::CustomMessageBox)
{
ui->setupUi(this);
this->initStyle();
this->moveFormToCenter();
}

CustomMessageBox::~CustomMessageBox()
{
delete ui;
}

void CustomMessageBox::setMessage(const QString msg, int type)
{
switch (type) {
case 1:
{
ui->labIcoMain->setStyleSheet("border-image: url(:/images/image/msg_question.png);");
ui->lab_Title->setText("询问");
break;
}
case 2:
{
ui->labIcoMain->setStyleSheet("border-image: url(:/images/image/msg_error.png);");
ui->btnCancel->setVisible(false);
ui->lab_Title->setText("错误");
break;
}
default:
{
ui->labIcoMain->setStyleSheet("border-image: url(:/images/image/msg_info.png);");
ui->btnCancel->setVisible(false);
ui->lab_Title->setText("提示");
break;
}
}
ui->labInfo->setText(msg);
this->setWindowTitle(ui->lab_Title->text());
}

void CustomMessageBox::test()
{
qDebug() << "测试是否可以输出";
}

void CustomMessageBox::initStyle()
{
QFont localFont = this->font();
localFont.setPointSize(10);
localFont.setFamily("Microsoft YaHei");
this->setFont(localFont);
this->setWindowTitle(ui->lab_Title->text());
//设置窗体标题栏隐藏
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
//设置图标
ui->lab_Ico->setStyleSheet("border-image: url(:/images/image/home.png);");
ui->btnMenu_Close->setIcon(this->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
//关联关闭按钮
connect(ui->btnMenu_Close, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->btnCancel, SIGNAL(clicked()), this, SLOT(close()));
}

void CustomMessageBox::moveFormToCenter()
{
int width = this->width();
int height = this->height();
QDesktopWidget dwt;
int deskWidth = dwt.availableGeometry().width();
int deskHeight = dwt.availableGeometry().height();
QPoint movePoint(deskWidth / 2 - width / 2, deskHeight / 2 - height / 2);
this->move(movePoint);
}

void CustomMessageBox::on_btnOk_clicked()
{
done(1);
this->close();
}

custommessagebox.ui

省略

调用

void Widget::on_btnMeesageBox_clicked()
{
CustomMessageBox messageBox;
messageBox.setMessage("调用成功", 1);
messageBox.exec();
}


举报

相关推荐

0 条评论