Qt5 Widget 程序 Qt 库裁剪方法
Windows 平台
使用 windeployqt.exe
时拷贝的 Qt 动态库,有一些是可能没用到的。
- If dynamic OpenGL was used, then the archive must additionally contain
反之,如果没使用dynamic OpenGL
,以下文件可以删除,可在调用windeployqt.exe
时加--no-angle
和--no-opengl-sw
这两个参数。
QtWidgets/C++ 程序都不用 OpenGL,所以完全可以删除。
Component | File Name |
---|---|
ANGLE libraries | libEGL.dll, libGLESv2.dll |
HLSL compiler library for ANGLE | d3dcompiler_XX.dll |
OpenGL Software renderer library | opengl32sw.dll |
- 如果没有使用
SVG
的话,下面三个文件也可以删掉,一般用到SVG的程序比较少,大部分可以删除。
- iconengines\qsvgicon.dll
- imageformats\qsvg.dll
- Qt5Svg.dll
-
bearer\qgenericbearer.dll
一般 QWidget 程序,此文件可以删除 -
不考虑国际化的话,
translations
里面的所有翻译文件也可以删掉,可以加--no-translations
参数。
5. 如果 imageformats 目录里面有几种图像格式没用上,也可以删掉。我自己通常把整个目录都删掉,Qt已经编译了对PNG
的支持,能读写程序包含的图标、图片就够,其它格式不重要。
imageformats 目录不能随意删除,如果工程里面用到了gif文件,如果整个文件夹删除的话,导致解析gif失败。
另外,这个整个文件夹也没多大,大概1.27M,所以不用删了。
6. styles\qwindowsvistastyle.dll
参考 Qt QWindowsVistaStyle Class
The QWindowsVistaStyle class provides a look and feel suitable for applications on Microsoft Windows Vista.
Warning: This style is only available on the Windows Vista platform because it makes use of Windows Vista's style engine.
因此,窃以为 Windows Vista 系统市场占有率非常非常少,所以可以去掉。
注意:实际去掉后,发现有问题,使用 QSS 的按钮上的文字变成镌刻效果了,不是全部,是部分(发现一个按钮),所以不能去掉(2022年4月22日)
综上所述,可以删除的文件有:
可以删除的文件 | 条件 |
---|---|
libEGL.dll libGLESv2.dll d3dcompiler_XX.dll opengl32sw.dll | 程序没有使用 dynamic OpenGL(QtWidgets/C++ 程序都不用 OpenGL) |
iconengines\qsvgicon.dll imageformats\qsvg.dll Qt5Svg.dll | 程序没有使用 SVG 格式文件(大部分程序都不会用到 SVG) |
translations\ 文件夹里面的所有文件 | 程序不考虑国际化 |
imageformats\ 文件夹里面的文件 | 程序没用到的图片格式就可以删除,一般程序都可以删除 |
以上文件合计25.3M,可以给程序减小相当大的体积了。
–no-compiler-runtime 参数
–no-compiler-runtime: Do not deploy compiler runtime (Desktop only).
这个只是不要拷贝编译器的运行时库安装文件,例如vcredist_x86.exe
。
如果没加这个参数,并且VCINSTALLDIR
环境变量也设置得正确的话,那么就会拷贝运行时库安装文件。
一般程序都是带上运行时库的DLL文件(如 msvcp140.dll),不会带安装文件的。
这个可以主动加上--no-compiler-runtime
这个参数让 windeployqt 不要拷贝运行时库安装文件,否则 windeployqt 会报警告:
Warning: Cannot find Visual Studio installation directory, VCINSTALLDIR is not set.
设置VCINSTALLDIR
环境变量的方法:
set VCINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC
设置完后再调用 windeployqt.exe。
参考文档
macOS 平台
macdeployqt
会加进来一些不会用到的模块,增加程序的体积,下面是对一个普通Widget
程序而言没用到的模块。可在运行完macdeployqt
后,用命令删除。
其他的模块都是需要的,删除会导致程序不能运行。
- rm -rf ./demo.app/Contents/Frameworks/QtVirtualKeyboard.framework
- rm -rf ./demo.app/Contents/Frameworks/QtQml.framework
- rm -rf ./demo.app/Contents/Frameworks/QtQmlModels.framework
- rm -rf ./demo.app/Contents/Frameworks/QtQuick.framework
- rm -rf ./demo.app/Contents/Frameworks/QtSvg.framework
- rm -rf ./demo.app/Contents/PlugIns/virtualkeyboard # 删除 virtualkeyboard 对应的 PlugIns 目录
以上文件合计27.8M,可以给程序减小相当大的体积了。