• Qt 将带界面的程序封装成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 if you use
    18 # any feature of Qt which as been marked as deprecated (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 from it.
    21 DEFINES += QT_DEPRECATED_WARNINGS
    22 
    23 # You can also make your code fail to compile if you use deprecated APIs.
    24 # In order to do so, uncomment the following line.
    25 # You can also select to 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 namespace Ui {
    13 class CustomMessageBox;
    14 }
    15 
    16 //class CustomMessageBox : public QDialog
    17 class CUSTOMMESSAGEBOXSHARED_EXPORT CustomMessageBox : public QDialog
    18 {
    19     Q_OBJECT
    20 
    21 public:
    22     explicit CustomMessageBox(QWidget *parent = 0);
    23     ~CustomMessageBox();
    24 
    25     void setMessage(const QString msg, int type);
    26 
    27     void test();
    28 
    29 private slots:
    30     void on_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(new Ui::CustomMessageBox)
    10 {
    11     ui->setupUi(this);
    12     this->initStyle();
    13     this->moveFormToCenter();
    14 }
    15 
    16 CustomMessageBox::~CustomMessageBox()
    17 {
    18     delete ui;
    19 }
    20 
    21 void CustomMessageBox::setMessage(const QString msg, int type)
    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 void CustomMessageBox::test()
    50 {
    51     qDebug() << "测试是否可以输出";
    52 }
    53 
    54 void CustomMessageBox::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 void CustomMessageBox::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 void CustomMessageBox::on_btnOk_clicked()
    83 {
    84     done(1);
    85     this->close();
    86 }

    custommessagebox.ui

    省略

    调用

    1 void Widget::on_btnMeesageBox_clicked()
    2 {
    3     CustomMessageBox messageBox;
    4     messageBox.setMessage("调用成功", 1);
    5     messageBox.exec();
    6 }
    
    
  • 相关阅读:
    ibatis集成封装之路(to mysql)
    设计模式第一弹—适配器模式
    markdown语法
    outlook vba开发要点
    PHP中json_encode中文编码的问题_学习
    isset、empty、var==null、is_null、var===null详细理解
    对冒泡和二分法,特别是二分法有了更深的理解
    php Socket表单提交学习一下
    php Socket模拟表单上传文件函数_学习
    java第九节 网络编程的基础知识
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14024700.html
Copyright © 2020-2023  润新知