当前位置: 首页 > news >正文

Qt从入门到入土(八) -打包Qt程序

前言

当你写完一个有趣的Qt程序时,想发给朋友或者家人,但是他们的电脑又没有安装Qt,那么如何直接在他们电脑上运行又不需要安装Qt呢?本篇文章会告诉你答案,本文详细的介绍了界面设计和功能实现。读完本文你不仅可以学会打包部署Qt程序而且还可以制作一个Qt打包程序的软件。

前期准备

创建Qt项目,CMakeLists.txt文件配置Qt。

#使用此项目版本不能低于3.20
cmake_minimum_required(VERSION 3.20)
#项目名称 版本 语言    
project(WindePloyGui VERSION 0.1 LANGUAGES CXX)   
#查找QT包
find_package(Qt6 REQUIRED COMPONENTS Widgets)
    
#设置变量
set(PROJECT_SOURCES
   main.cpp
   )    
#添加可执行文件
add_executable(WindePloyGui ${PROJECT_SOURCES})   
#添加Qt链接库
target_link_libraries(WindePloyGui Qt6::Widgets)  

创建main.cpp,构建项目查看是否配置错误。

#include <QApplication>
#include <QWidget>

int main(int argc, char* argv[]) 
{
    QApplication app(argc, argv);

    QWidget window;
    window.show();

    return app.exec();
}

创建WindePloyGui.h

#ifndef PACKAGEQTGUI_H
#define PACKAGEQTGUI_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
class PackageQtGui;
}
QT_END_NAMESPACE

class PackageQtGui : public QWidget
{
    Q_OBJECT

public:
    PackageQtGui(QWidget *parent = nullptr);
    ~PackageQtGui();

public:
    void initUI();
private:
    Ui::PackageQtGui *ui;
};
#endif // PACKAGEQTGUI_H

创建WindePloyGui.cpp

#include "packageqtgui.h"
#include "./ui_packageqtgui.h"

PackageQtGui::PackageQtGui(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PackageQtGui)
{
    ui->setupUi(this);
}

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

界面初始化

本次界面使用到了QFormLayout,QComboBox等控件。

QFormLayout:QFormLayout 是 Qt 提供的一种布局工具,专门用来生成类似表单的界面。它允许您将控件以“标签-控件”对的形式排列,通常用于输入表单、设置界面等场景。

QComboBox:创建下拉列表,用户可以从列表中选择一个选项。

    this->m_qtVersionListCmb= new QComboBox;
    this->m_qtKitsListCmb = new QComboBox;

    //给QComboBox添加项

    //this->m_qtVersionListCmb->addItems({"one","two","three"});

    auto flayout = new QFormLayout;
    flayout->addRow("选择Qt版本",this->m_qtVersionListCmb);
    flayout->addRow("选择Qt编译套件",this->m_qtKitsListCmb);

当前初始化UI代码

void PackageQtGui::initUI()
{
    this->m_qtVersionListCmb= new QComboBox;
    this->m_qtKitsListCmb = new QComboBox;

    auto selectExeBtn = new QPushButton("选择exe文件");
    auto createBtn = new QPushButton("生成");
    auto testBtn = new QPushButton("测试");
    auto aboutBtn = new QPushButton("关于");

    auto flayout = new QFormLayout;
    flayout->addRow("选择Qt版本",this->m_qtVersionListCmb);
    flayout->addRow("选择Qt编译套件",this->m_qtKitsListCmb);

    auto hlayout = new QHBoxLayout;
    hlayout->addWidget(testBtn);
    hlayout->addWidget(aboutBtn);

    auto vlayout = new QVBoxLayout(this);
    vlayout->addLayout(flayout);
    vlayout->addWidget(selectExeBtn);
    vlayout->addWidget(createBtn);
    vlayout->addLayout(hlayout);

}

当前效果

调整控件大小

使用setSizePolicy函数来调整控件大小。

 selectExeBtn->setSizePolicy(QSizePolicy::Policy::Expanding,QSizePolicy::Policy::Expanding);

美化QComboBox中的drop-down

有时直接给子控件设置图片可能会因为QComboBox::drop-down 的显示区域可能太小,导致图片无法完全显示。可以通过设置 widthheight 来调整显示区域。

QComboBox::drop-down{
image:url(':/Recourses/Icons/drop-down.png');
subcontrol-origin: padding;
subcontrol-position: right center;
width: 20px; /* 调整箭头宽度 */
height: 20px; /* 调整箭头高度 */
}

QComboBox::drop-down:hover{
image:url(':/Recourses/Icons/drop-down-hover.png');
}

QComboBox::drop-down:checked{
image:url(':/Recourses/Icons/drop-down-on.png');
}

QComboBox::drop-down:checked:hover{
image:url(':/Recourses/Icons/drop-down-on-hover.png');
}

初始化UI代码

void PackageQtGui::initUI()
{
    this->setWindowTitle("QT程序打包工具");
    this->setFixedSize(640,510);
    this->m_qtVersionListCmb= new QComboBox;
    this->m_qtKitsListCmb = new QComboBox;

    auto selectExeBtn = new QPushButton("选择exe文件");
    auto createBtn = new QPushButton("生成");
    auto testBtn = new QPushButton("测试");
    auto aboutBtn = new QPushButton("关于");

    selectExeBtn->setObjectName("selectExeBtn");
    selectExeBtn->setSizePolicy(QSizePolicy::Policy::Expanding,QSizePolicy::Policy::Expanding);

    auto flayout = new QFormLayout;
    flayout->addRow("选择 Qt 版本",this->m_qtVersionListCmb);
    flayout->addRow("选择编译套件",this->m_qtKitsListCmb);

    auto hlayout = new QHBoxLayout;
    hlayout->addWidget(testBtn);
    hlayout->addWidget(aboutBtn);

    auto vlayout = new QVBoxLayout(this);
    vlayout->addLayout(flayout);
    vlayout->addWidget(selectExeBtn);
    vlayout->addWidget(createBtn);
    vlayout->addLayout(hlayout);
}

css界面美化代码

/*通用的样式*/
*{
background-color:white;
font:normal 15px "楷体";
}

QPushButton,QComboBox{
border:1px solid rgb(213,213,213);
border-radius:8px;
}

QComboBox:hover{
border-color:rgb(100,100,100);
}

QComboBox::drop-down{
image:url(':/Recourses/Icons/drop-down.png');
subcontrol-origin: padding;
subcontrol-position: right center;
width: 20px; /* 调整箭头宽度 */
height: 20px; /* 调整箭头高度 */
}

QComboBox::drop-down:hover{
image:url(':/Recourses/Icons/drop-down-hover.png');
}

QComboBox::drop-down:checked{
image:url(':/Recourses/Icons/drop-down-on.png');
}

QComboBox::drop-down:checked:hover{
image:url(':/Recourses/Icons/drop-down-on-hover.png');
}

QPushButton:hover{
background:rgb(220,220,220);
}

/*设置特定样式*/
QPushButton#selectExeBtn{
border-width:2px;
font:italic 20px "楷体";
color:rgb(174,174,174);
}

功能实现

首先,找到Qt版本编译的套件

构建QtEnvSearch类用于获取系统的应用程序可写路径,因为不同的电脑路径不同但可以通过QStandardPaths来获取到当前电脑的路径

QString path = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);

查找Qt版本的文件夹,获取一个目录中所有以 "Qt" 开头的子目录的信息列表

QFileInfoList info_list = dir.entryInfoList({"Qt*"},QDir::Dirs);

筛选出不为空的文件夹

for(auto& info:info_list)
    {
        //筛选出系统路径中名称带有Qt的文件夹
        // 检查文件夹是否为空
        QDir subDir(info.absoluteFilePath());
        if (!subDir.isEmpty()) {
            qDebug() << "Non-empty Qt version folder:" << info.absoluteFilePath();
            infoList.append(info.absoluteFilePath());
        }
    }

再次筛选Qt目录中Qt版本号文件夹,并排除空文件夹

QStringList resultList;
    for(auto &info:infoList)
    {
        //QDir::Dirs 表示只获取子目录。
        //QDir::NoDotAndDotDot 是一个常用的过滤器,用于排除 . 和 .. 这两个特殊目录。
        QFileInfoList sub_info_list = QDir(info).entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
        for(auto& i:sub_info_list)
        {
            QDir subDir(i.absoluteFilePath());
            if(!subDir.isEmpty())
            {
                resultList.append(i.absoluteFilePath());
            }
        }
    }

获取到所选版本目录中的Qt编译套件

QStringList QtEnvSearch::m_qtKitsLists()
{
    if(m_currentVersion.isEmpty())
    {
        m_currentVersion = m_versionMap.firstKey();
    }

    auto path = m_versionMap.value(m_currentVersion);
    QDir dir(path);
    if(!dir.exists())
    {
        qWarning()<<path<<"not exists!";
        return QStringList{};
    }

    return dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
}

把获取到的版本号和套件列表添加到QComboBox的item中

this->m_qtVersionListCmb->addItems({m_envSearch.m_qtVersionLists()});
this->m_qtKitsListCmb->addItems({m_envSearch.m_qtKitsLists()});

当选择的Qt版本改变时需要更新所对应的选择编译套件

connect(this->m_qtVersionListCmb,&QComboBox::currentTextChanged,[=](const QString& ver)
    {
        this->m_envSearch.setCurrentVersion(ver);
        this->m_qtKitsListCmb->clear();
        this->m_qtKitsListCmb->addItems(this->m_envSearch.m_qtKitsLists());
    });

当选择编译套件发生改变的时候需要更新对应的Qt版本

connect(this->m_qtKitsListCmb,&QComboBox::currentTextChanged,&this->m_envSearch,&QtEnvSearch::setCurrentKits);

当前效果

接下来要实现selectExeBtn,点击该按钮即可打开文件(这里使用QFileDialog),并将按钮文本修改成获取到的文件名。

connect(selectExeBtn,&QPushButton::clicked,[=](){
        auto filename = QFileDialog::getOpenFileName(this,"选择exe文件",QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)
                 ,"exe(*.exe);;all(*.*)");
        if(filename.isEmpty())
            return;
        selectExeBtn->setText(QFileInfo(filename).fileName());
        qDebug()<<filename;

    });

之后,点击生成按钮完成部署。如何完成部署呢?首先进入到套件目录,根据快捷方式找到指定的bin目录。

bool QtEnvSearch::generate()
{
    //进入套件目录
    QDir dir(m_versionMap.value(m_currentVersion));
    if(!dir.cd(m_currentKits))
    {
        qWarning()<<"cd"<<m_currentKits<<"failed~";
        return false;
    }

    //找到一个快捷方式,并进入指向的目标
    auto all_entry = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries);
    if(all_entry.isEmpty())
    {
        qWarning()<<"生成失败,套件无效";
        return false;
    }
    //qDebug()<<all_entry;

    //获取qt的bin目录
    auto qtBin = QFileInfo(all_entry.first().symLinkTarget()).path();
    //qDebug()<<qtBin;
    return true;
}

使用system函数进行部署

// 传入qtbin目录中的windeployqt.exe和待部署的exe文件的路径

system(QString(qtBin + "/windeployqt.exe " + m_exeFile).toStdString().data());

使用QProcess::startDetached进行部署

bool success = QProcess::startDetached(QString(qtBin + "/windeployqt.exe"),{m_exeFile});

点击测试按钮打开程序

    // 同样使用QProcess::startDetached来打开EXE程序
    return QProcess::startDetached(m_exeFile);

至此,打包Qt程序的软件就已经完成了,当然还有一个关于按钮你可以写上你的信息表明你是创作者或是对这个软件的介绍,这里就不一一赘述了。

相关文章:

  • 通义万相 2.1 × 蓝耘智算:AIGC 界的「黄金搭档」如何重塑创作未来?
  • 18天 - 常见的 HTTP 状态码有哪些?HTTP 请求包含哪些内容,请求头和请求体有哪些类型?HTTP 中 GET 和 POST 的区别是什么?
  • 刷题统计 | 第十三届蓝桥杯省赛C++B组
  • AI写论文提示词指令大全,快速写论文
  • 数据库学习笔记
  • 系统架构设计师-第5章 计算机网络
  • 我们在开发时,什么时候用到虚函数和纯虚函数?
  • 修复ubuntu下找不到音频设备的问题
  • Docker开发工具安装大合集
  • 如何撰写专业技术书籍的序言?——完整指南
  • FreeRTOS概述
  • SpringBoot 接入 豆包 火山方舟大模型
  • 共享内存的原理和创建
  • #函数探幽
  • 《深度解析DeepSeek-M8:量子经典融合,重塑计算能效格局》
  • Zemax 中的 CAD 文件性能比较
  • LeetCode hot 100—爬楼梯
  • Android 线程池实战指南:高效管理多线程任务
  • leetcode日记(91)二叉树的最大深度
  • E1-106.租车骑绿道(贪心)
  • 西安机场回应航站楼“水帘洞”事件:屋面排水系统被冰雹堵塞
  • 中国证监会印发2025年度立法工作计划
  • 雇来的“妈妈”:为入狱雇主无偿带娃4年,没做好准备说再见
  • 海南省三亚市委原常委、秘书长黄兴武被“双开”
  • 警惕“全网最低价”等宣传,市监总局和中消协发布直播消费提示
  • 重庆党政代表团在沪考察,陈吉宁龚正与袁家军胡衡华共商两地深化合作工作