• Windows下Qt5程序打包发布


    Windows下Qt5程序打包发布与图标设置

    原文(有删改):https://blog.csdn.net/qq_39105333/article/details/114779650

    设置程序图标

    默认的程序是没有图标的,丑丑的。

    设置桌面图标

    0、选择一个你喜欢的ico图片文件,放在源码同级路径

    1、在程序的.pro文件中添加下列代码:

    RC_ICONS = app.ico //设置图标 或  RC_FILE = app.rc 
    #通过设置系统变量 VERSION 或 RC_ICONS (至少一个),qmake 会自动生成 .rc 文件。
    
    # 指定图标(等号右侧是你的ico文件名称)
    RC_ICONS = let-me-know.ico
    # 设置程序版本号,可省略
    VERSION = 1.0.0.0
    

    重新编译运行程序,此后,我们就可以看到程序运行图标和程序桌面图标都变成了我们设置的ico图标。

    代码中单独设置程序运行图标

    如果希望程序图标和桌面图标不一样的话,那么在项目源文件的构造函数内添加如下代码:

    #define SHOW_ICO ":/source/image/tray.jpg"
    setWindowIcon(QIcon(SHOW_ICO));
    

    重新编译运行程序之后,程序的运行图标就变成了我们设置的图标,不过这种办法无法对程序桌面图标进行设置。

    使用windeployqt 提取dll与打包exe

    这里以:QT Creator为例。

    0、设置项目以Release编译生成:

    比如生成了:helloQt.exe

    1、找到项目的生成目录,比如:

    • 项目源码路径:D:let-me-know
    • 项目生成目录是 D:let-me-knowRelease

    2、 进入生成路径中,找到 helloQt.exe,将这个exe 复制到一个新的单独的文件夹里用于发布

    • 比如:存到 D:let-me-know-bin 文件夹里面,此时打开helloQt.exe会提示缺少dll之类的信息,程序打不开。

    3、打开 “开始”菜单中打开Qt命令行工具,比如:Qt 5.12.5

    4、输入下列命令:

    Setting up environment for Qt usage...
    Remember to call vcvarsall.bat to complete environment setup!
    
    # 切换到 D 盘
    C:QtQt5.12.55.12.5msvc2015_64>d:
    
    # 进入` D:let-me-know-bin\` 
    D:>cd D:let-me-know-bin
    
    # 打包(常规程序)
    D:>windeployqt let-me-know.exe
    
    # 打包(QML),
    ## 将`D:QtQt5.4.05.4mingw491_32qml`替换为你自己的Qt安装目录的qml文件夹路径。
    ## 如果发布出现问题,将上述路径改为你自己程序`.pro`文件所在路径。
    D:>windeployqt let-me-know.exe --qmldir C:QtQt5.12.55.12.5msvc2015_64qml
    

    之后会看到命令行进行了一系列操作,

    D:let-me-know-bin>windeployqt let-me-know.exe
    D:let-me-know-binlet-me-know.exe 64 bit, release executable
    Adding Qt5Svg for qsvgicon.dll
    Direct dependencies: Qt5Core Qt5Gui Qt5Network Qt5Widgets
    All dependencies   : Qt5Core Qt5Gui Qt5Network Qt5Widgets
    To be deployed     : Qt5Core Qt5Gui Qt5Network Qt5Svg Qt5Widgets
    Warning: Cannot find Visual Studio installation directory, VCINSTALLDIR is not set.
    Updating Qt5Core.dll.
    Updating Qt5Gui.dll.
    Updating Qt5Network.dll.
    Updating Qt5Svg.dll.
    Updating Qt5Widgets.dll.
    Updating libGLESV2.dll.
    Updating libEGL.dll.
    Updating D3Dcompiler_47.dll.
    Updating opengl32sw.dll.
    Patching Qt5Core.dll...
    Creating directory D:/bin1/bearer.
    Updating qgenericbearer.dll.
    Creating directory D:/bin1/iconengines.
    Updating qsvgicon.dll.
    Creating directory D:/bin1/imageformats.
    Updating qgif.dll.
    Updating qicns.dll.
    Updating qico.dll.
    // ...
    Creating qt_pl.qm...
    Creating qt_ru.qm...
    Creating qt_sk.qm...
    Creating qt_uk.qm...
    Creating qt_zh_TW.qm...
    

    随后,D:helloQt文件夹中会出现helloQt.exe运行所需文件,如果你的QT程序没有依赖第三方库,那么此时打开helloQt.exe程序就可以直接运行了。

    基于Engima Virtual Box发布exe

    Q:这个时候,我们的程序如果需要发布,必须将整个文件夹发出去,显得有些繁重,有没有解决方案呢?

    A: 使用Engima Virtual Box将我们的这个文件夹压缩为一个可执行文件。

    下载

    Engima Virtual Boxhttps://enigmaprotector.com/en/downloads.html

    选择:Enigma Virtual Box

    Version 9.60 FREEWARE (versions history),Size 6.9 Mb

    安装,没有特殊说明。

    运行

    指定要发布的exe

    指定要打包的exe,就会默认指定输出的exe

    image-20210522165150363

    添加依赖文件

    就是库等东西。

    直接指定D:let-me-know-bin

    点击确定,选择Add Folder %DEFAULT FOLDER%,效果如下:

    当然,我们也可以选择将文件和文件夹一起拖进来切记!一定要将可执行程序同目录所有文件及文件夹加进来!

    注:如果使用到了一些配置(例如ini),为了方便修改,这里建议不将对应的资源文件打包进去。

    压缩

    为了减小程序体积,我们这里选择压缩,点击右下角的Files Options

    • 勾选Compress Files
    • 点击OK

    image-20210522165650528

    Process

    最后,点击窗口右下角的Process

    片刻之后,就可以在我们的输出路径看到生成的EXE文件了,我们将新生成的EXE文件移动到其他没有运行环境的文件夹中,打开,程序正常运行。

    附录:Qt windeployqt帮助说明

    中文帮助

    用法:windeployqt [options] [files]
    Qt部署工具5.9.0
    
    使用windeployqt最简单的方法是添加Qt的bin目录
    安装(例如<QT_DIR  bin>)到PATH变量,然后运行:
      windeployqt <path-to-app-binary>
    如果ICU,ANGLE等不在bin目录中,则需要在PATH中
    变量。如果您的应用程序使用Qt Quick,请运行:
      windeployqt --qmldir <path-to-app-qml-files> <path-to-app-binary>
    
    选项:
      -  ?, -h,--help显示此帮助。
      -v,--version显示版本信息。
      --dir <目录>使用目录而不是二进制目录。
      --libdir <path>将库复制到路径。
      --plugindir <path>将插件复制到路径。
      --debug假设调试二进制文件。
      - 释放假设释放二进制文件。
      --pdb部署.pdb文件(MSVC)。
      - 强制更新文件。
      - 慢跑模拟模式。正常行为,但不要
                                复制/更新任何文件。
      --no-patchqt不要修补Qt5Core库。
      --no-plugins跳过插件部署。
      --no-libraries跳过库部署。
      --qmldir <directory>从目录开始扫描QML-导入。
      --no-quick-import跳过Qt Quick导入的部署。
      --no-translations跳过翻译的部署。
      --no-system-d3d-compiler跳过系统D3D编译器的部署。
      --compiler-runtime部署编译器运行时(仅限桌面)。
      --no-compiler-runtime不要部署编译器运行时(仅限桌面)。
      --webkit2部署WebKit2(web进程)。
      --no-webkit2跳过WebKit2的部署。
      --json以JSON格式打印到stdout。
      - 角度的角度部署。
      - 无角度禁用ANGLE的部署。
      --no-opengl-sw不要部署软件光栅化程序库。
      --list <选项>只打印复制文件的名称。
                                可用选项:
                                 source:源文件的绝对路径
                                 目标:目标文件的绝对路径
                                 relative:相对目标文件的路径
                                           到目标目录
                                 映射:输出源和相对
                                           目标,适合在一个
                                           Appx映射文件
      --verbose <level>详细级别。
    
    可以通过传递它们的名称(-xml)或通过传递来删除Qt库
    该名称由--no-(--no-xml)前缀。可用的库:
    蓝牙并发核心声明式设计器设计者组件
    enginio gui qthelp多媒体multimediawidgets multimediaquick网络nfc
    opengl定位printsupport qml qmltooling快速quickparticles quickwidgets
    脚本scripttools传感器serialport sql svg测试webkit webkitwidgets
    websockets widgets winextras xml xmlpatterns webenginecore webengine
    webenginewidgets 3dcore 3drenderer 3dquick 3dquickrenderer 3dinput geoservices
    webchannel texttospeech serialbus
    
    参数:
      [files]包含二进制文件的二进制文件或目录。
    

    英文帮助

    C:QtQt5.12.55.12.5msvc2015_64>windeployqt --help
    Usage: windeployqt [options] [files]
    Qt Deploy Tool 5.12.5
    
    The simplest way to use windeployqt is to add the bin directory of your Qt
    installation (e.g. <QT_DIRin>) to the PATH variable and then run:
      windeployqt <path-to-app-binary>
    If ICU, ANGLE, etc. are not in the bin directory, they need to be in the PATH
    variable. If your application uses Qt Quick, run:
      windeployqt --qmldir <path-to-app-qml-files> <path-to-app-binary>
    
    Options:
      -?, -h, --help            Displays this help.
      -v, --version             Displays version information.
      --dir <directory>         Use directory instead of binary directory.
      --libdir <path>           Copy libraries to path.
      --plugindir <path>        Copy plugins to path.
      --debug                   Assume debug binaries.
      --release                 Assume release binaries.
      --pdb                     Deploy .pdb files (MSVC).
      --force                   Force updating files.
      --dry-run                 Simulation mode. Behave normally, but do not
                                copy/update any files.
      --no-patchqt              Do not patch the Qt5Core library.
      --no-plugins              Skip plugin deployment.
      --no-libraries            Skip library deployment.
      --qmldir <directory>      Scan for QML-imports starting from directory.
      --no-quick-import         Skip deployment of Qt Quick imports.
      --no-translations         Skip deployment of translations.
      --no-system-d3d-compiler  Skip deployment of the system D3D compiler.
      --compiler-runtime        Deploy compiler runtime (Desktop only).
      --no-compiler-runtime     Do not deploy compiler runtime (Desktop only).
      --webkit2                 Deployment of WebKit2 (web process).
      --no-webkit2              Skip deployment of WebKit2.
      --json                    Print to stdout in JSON format.
      --angle                   Force deployment of ANGLE.
      --no-angle                Disable deployment of ANGLE.
      --no-opengl-sw            Do not deploy the software rasterizer library.
      --list <option>           Print only the names of the files copied.
                                Available options:
                                 source:   absolute path of the source files
                                 target:   absolute path of the target files
                                 relative: paths of the target files, relative
                                           to the target directory
                                 mapping:  outputs the source and the relative
                                           target, suitable for use within an
                                           Appx mapping file
      --verbose <level>         Verbose level (0-2).
    
    Qt libraries can be added by passing their name (-xml) or removed by passing
    the name prepended by --no- (--no-xml). Available libraries:
    bluetooth concurrent core declarative designer designercomponents enginio
    gamepad gui qthelp multimedia multimediawidgets multimediaquick network nfc
    opengl positioning printsupport qml qmltooling quick quickparticles quickwidgets
    script scripttools sensors serialport sql svg test webkit webkitwidgets
    websockets widgets winextras xml xmlpatterns webenginecore webengine
    webenginewidgets 3dcore 3drenderer 3dquick 3dquickrenderer 3dinput 3danimation
    3dextras geoservices webchannel texttospeech serialbus webview
    
    Arguments:
      [files]                   Binaries or directory containing the binary.
    
    如果说我的文章对你有用,只不过是我站在巨人的肩膀上再继续努力罢了。
    若在页首无特别声明,本篇文章由 Schips 经过整理后发布。
    博客地址:https://www.cnblogs.com/schips/
  • 相关阅读:
    KL散度、JS散度和交叉熵
    np.dot()计算两个变量的乘积
    numpy 相关统计
    sns.FacetGrid(),map用法
    df.to_dict()转化为字典数据
    springboot 热部署
    Springboot thymeleaf
    springboot 静态资源
    springboot yaml part2
    Warning: You cannot set a form field before rendering a field associated with the value. ant desgin pro form 表单报错
  • 原文地址:https://www.cnblogs.com/schips/p/deploy_qt5_binary_in_windows.html
Copyright © 2020-2023  润新知