Qt5NSISWindows 安装程序·

使用 NSIS 为 Qt 项目创建安装包

ronger

ronger

304 0

前情提要

RYCOM 是 Hugh 开发的一款开源串口调试助手,使用 QT 设计,C++ 编写。Hugh 之前使用的打包工具打包出来的安装包存在以下问题:

  1. 无法注册系统注册表, 实现记录历史安装目录
  2. 重新安装无法覆盖历史文件

本教程将基于 NSIS 和 HW VNIS Edit 实现现代化 UI 的安装包打包, 并修复以上问题。

前期准备

  1. 编译好的 Qt 项目
  2. 应用程序 ico 文件
  3. 软件授权协议 Licence.txt

使用教程

步骤 1:安装 NSIS

  1. 下载 NSIS 最新版
  2. 安装后,确保 makensis 命令可用(会添加到系统环境变量)。

步骤 2:安装 HW VNIS Edit

  1. 下载 HM VNIS Edit 最新版

步骤 3:编写 NSIS 脚本

  1. 打开 HM VNIS Edit
  2. 新建脚本:向导

    • 设置应用程序基本信息
    • 设置安装程序所用选项
    • 设置应用程序的目录和授权信息
    • 添加应用程序文件

    • 设置应用程序图标和快捷方式
    • 设置安装完成后执行的操作
    • 设置卸载程序配置
    • 保存脚本并编译
    • 保存脚本并编译完成后你将在选择保存的目录下看到 installer.nsiRYCOMSetup.exe 文件

生成的脚本内容

; Script generated by the HM NIS Edit Script Wizard.

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "RYCOM"
!define PRODUCT_VERSION "2.6.0"
!define PRODUCT_PUBLISHER "RYMCU"
!define PRODUCT_WEB_SITE "https://rymcu.com"
!define PRODUCT_DIR_REGKEY "Software//Microsoft//Windows//CurrentVersion//App Paths//maintenancetool.exe"
!define PRODUCT_UNINST_KEY "Software//Microsoft//Windows//CurrentVersion//Uninstall//${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

; MUI 1.67 compatible ------
!include "MUI.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "logo.ico"
!define MUI_UNICON "${NSISDIR}//Contrib//Graphics//Icons//modern-uninstall.ico"

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!define MUI_LICENSEPAGE_CHECKBOX
!insertmacro MUI_PAGE_LICENSE "licence//Licence.txt"
; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!define MUI_FINISHPAGE_RUN "$INSTDIR//RYCOM.exe"
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "RYCOMSetup.exe"
InstallDir "$PROGRAMFILES//RYMCU//RYCOM"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show

Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite try
  File "..//RYCOM2.6.0//components.xml"
  File "..//RYCOM2.6.0//D3Dcompiler_47.dll"
  SetOutPath "$INSTDIR//iconengines"
  File "..//RYCOM2.6.0//iconengines//qsvgicon.dll"
  SetOutPath "$INSTDIR//imageformats"
  File "..//RYCOM2.6.0//imageformats//qgif.dll"
  File "..//RYCOM2.6.0//imageformats//qicns.dll"
  File "..//RYCOM2.6.0//imageformats//qico.dll"
  File "..//RYCOM2.6.0//imageformats//qjpeg.dll"
  File "..//RYCOM2.6.0//imageformats//qsvg.dll"
  File "..//RYCOM2.6.0//imageformats//qtga.dll"
  File "..//RYCOM2.6.0//imageformats//qtiff.dll"
  File "..//RYCOM2.6.0//imageformats//qwbmp.dll"
  File "..//RYCOM2.6.0//imageformats//qwebp.dll"
  SetOutPath "$INSTDIR"
  File "..//RYCOM2.6.0//InstallationLog.txt"
  File "..//RYCOM2.6.0//installer.dat"
  SetOutPath "$INSTDIR//installerResources//com.RYMCU.RYCOM"
  File "..//RYCOM2.6.0//installerResources//com.RYMCU.RYCOM//2.6.0content.txt"
  File "..//RYCOM2.6.0//installerResources//com.RYMCU.RYCOM//2.6.0iconengines.txt"
  File "..//RYCOM2.6.0//installerResources//com.RYMCU.RYCOM//2.6.0imageformats.txt"
  File "..//RYCOM2.6.0//installerResources//com.RYMCU.RYCOM//2.6.0platforms.txt"
  File "..//RYCOM2.6.0//installerResources//com.RYMCU.RYCOM//2.6.0translations.txt"
  SetOutPath "$INSTDIR"
  File "..//RYCOM2.6.0//libEGL.dll"
  File "..//RYCOM2.6.0//libgcc_s_dw2-1.dll"
  File "..//RYCOM2.6.0//libGLESV2.dll"
  File "..//RYCOM2.6.0//libstdc++-6.dll"
  File "..//RYCOM2.6.0//libwinpthread-1.dll"
  File "..//RYCOM2.6.0//maintenancetool.dat"
  File "..//RYCOM2.6.0//maintenancetool.exe"
  File "..//RYCOM2.6.0//maintenancetool.ini"
  File "..//RYCOM2.6.0//network.xml"
  File "..//RYCOM2.6.0//opengl32sw.dll"
  SetOutPath "$INSTDIR//platforms"
  File "..//RYCOM2.6.0//platforms//qwindows.dll"
  SetOutPath "$INSTDIR"
  File "..//RYCOM2.6.0//Qt5Core.dll"
  File "..//RYCOM2.6.0//Qt5Gui.dll"
  File "..//RYCOM2.6.0//Qt5SerialPort.dll"
  File "..//RYCOM2.6.0//Qt5Svg.dll"
  File "..//RYCOM2.6.0//Qt5Widgets.dll"
  File "..//RYCOM2.6.0//RYCOM.exe"
  CreateDirectory "$SMPROGRAMS//RYCOM"
  CreateShortCut "$SMPROGRAMS//RYCOM//RYMCU.lnk" "$INSTDIR//RYCOM.exe"
  CreateShortCut "$DESKTOP//RYMCU.lnk" "$INSTDIR//RYCOM.exe"
  SetOutPath "$INSTDIR//translations"
  File "..//RYCOM2.6.0//translations//qt_bg.qm"
  File "..//RYCOM2.6.0//translations//qt_ca.qm"
  File "..//RYCOM2.6.0//translations//qt_cs.qm"
  File "..//RYCOM2.6.0//translations//qt_da.qm"
  File "..//RYCOM2.6.0//translations//qt_de.qm"
  File "..//RYCOM2.6.0//translations//qt_en.qm"
  File "..//RYCOM2.6.0//translations//qt_es.qm"
  File "..//RYCOM2.6.0//translations//qt_fi.qm"
  File "..//RYCOM2.6.0//translations//qt_fr.qm"
  File "..//RYCOM2.6.0//translations//qt_gd.qm"
  File "..//RYCOM2.6.0//translations//qt_he.qm"
  File "..//RYCOM2.6.0//translations//qt_hu.qm"
  File "..//RYCOM2.6.0//translations//qt_it.qm"
  File "..//RYCOM2.6.0//translations//qt_ja.qm"
  File "..//RYCOM2.6.0//translations//qt_ko.qm"
  File "..//RYCOM2.6.0//translations//qt_lv.qm"
  File "..//RYCOM2.6.0//translations//qt_pl.qm"
  File "..//RYCOM2.6.0//translations//qt_ru.qm"
  File "..//RYCOM2.6.0//translations//qt_sk.qm"
  File "..//RYCOM2.6.0//translations//qt_uk.qm"
SectionEnd

Section -AdditionalIcons
  SetOutPath $INSTDIR
  WriteIniStr "$INSTDIR//${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  CreateShortCut "$SMPROGRAMS//RYCOM//Website.lnk" "$INSTDIR//${PRODUCT_NAME}.url"
  CreateShortCut "$SMPROGRAMS//RYCOM//Uninstall.lnk" "$INSTDIR//uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR//uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR//maintenancetool.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR//uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR//maintenancetool.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从你的计算机移除。"
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "你确实要完全移除 $(^Name) ,其及所有的组件?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  Delete "$INSTDIR//${PRODUCT_NAME}.url"
  Delete "$INSTDIR//uninst.exe"
  Delete "$INSTDIR//translations//qt_uk.qm"
  Delete "$INSTDIR//translations//qt_sk.qm"
  Delete "$INSTDIR//translations//qt_ru.qm"
  Delete "$INSTDIR//translations//qt_pl.qm"
  Delete "$INSTDIR//translations//qt_lv.qm"
  Delete "$INSTDIR//translations//qt_ko.qm"
  Delete "$INSTDIR//translations//qt_ja.qm"
  Delete "$INSTDIR//translations//qt_it.qm"
  Delete "$INSTDIR//translations//qt_hu.qm"
  Delete "$INSTDIR//translations//qt_he.qm"
  Delete "$INSTDIR//translations//qt_gd.qm"
  Delete "$INSTDIR//translations//qt_fr.qm"
  Delete "$INSTDIR//translations//qt_fi.qm"
  Delete "$INSTDIR//translations//qt_es.qm"
  Delete "$INSTDIR//translations//qt_en.qm"
  Delete "$INSTDIR//translations//qt_de.qm"
  Delete "$INSTDIR//translations//qt_da.qm"
  Delete "$INSTDIR//translations//qt_cs.qm"
  Delete "$INSTDIR//translations//qt_ca.qm"
  Delete "$INSTDIR//translations//qt_bg.qm"
  Delete "$INSTDIR//RYCOM.exe"
  Delete "$INSTDIR//Qt5Widgets.dll"
  Delete "$INSTDIR//Qt5Svg.dll"
  Delete "$INSTDIR//Qt5SerialPort.dll"
  Delete "$INSTDIR//Qt5Gui.dll"
  Delete "$INSTDIR//Qt5Core.dll"
  Delete "$INSTDIR//platforms//qwindows.dll"
  Delete "$INSTDIR//opengl32sw.dll"
  Delete "$INSTDIR//network.xml"
  Delete "$INSTDIR//maintenancetool.ini"
  Delete "$INSTDIR//maintenancetool.exe"
  Delete "$INSTDIR//maintenancetool.dat"
  Delete "$INSTDIR//libwinpthread-1.dll"
  Delete "$INSTDIR//libstdc++-6.dll"
  Delete "$INSTDIR//libGLESV2.dll"
  Delete "$INSTDIR//libgcc_s_dw2-1.dll"
  Delete "$INSTDIR//libEGL.dll"
  Delete "$INSTDIR//installerResources//com.RYMCU.RYCOM//2.6.0translations.txt"
  Delete "$INSTDIR//installerResources//com.RYMCU.RYCOM//2.6.0platforms.txt"
  Delete "$INSTDIR//installerResources//com.RYMCU.RYCOM//2.6.0imageformats.txt"
  Delete "$INSTDIR//installerResources//com.RYMCU.RYCOM//2.6.0iconengines.txt"
  Delete "$INSTDIR//installerResources//com.RYMCU.RYCOM//2.6.0content.txt"
  Delete "$INSTDIR//installer.dat"
  Delete "$INSTDIR//InstallationLog.txt"
  Delete "$INSTDIR//imageformats//qwebp.dll"
  Delete "$INSTDIR//imageformats//qwbmp.dll"
  Delete "$INSTDIR//imageformats//qtiff.dll"
  Delete "$INSTDIR//imageformats//qtga.dll"
  Delete "$INSTDIR//imageformats//qsvg.dll"
  Delete "$INSTDIR//imageformats//qjpeg.dll"
  Delete "$INSTDIR//imageformats//qico.dll"
  Delete "$INSTDIR//imageformats//qicns.dll"
  Delete "$INSTDIR//imageformats//qgif.dll"
  Delete "$INSTDIR//iconengines//qsvgicon.dll"
  Delete "$INSTDIR//D3Dcompiler_47.dll"
  Delete "$INSTDIR//components.xml"

  Delete "$SMPROGRAMS//RYCOM//Uninstall.lnk"
  Delete "$SMPROGRAMS//RYCOM//Website.lnk"
  Delete "$DESKTOP//RYMCU.lnk"
  Delete "$SMPROGRAMS//RYCOM//RYMCU.lnk"

  RMDir "$SMPROGRAMS//RYCOM"
  RMDir "$INSTDIR//translations"
  RMDir "$INSTDIR//platforms"
  RMDir "$INSTDIR//installerResources//com.RYMCU.RYCOM"
  RMDir "$INSTDIR//imageformats"
  RMDir "$INSTDIR//iconengines"
  RMDir "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  SetAutoClose true
SectionEnd

通过以上步骤,您可以为 Qt 项目生成一个专业的 Windows 安装程序。

参考文章

相关文章

优先推荐同专题、同标签和同作者内容,补足热门文章。

评论 0

登录 后参与评论

评论

成为第一个评论的人