利用Inno Setup打包Qt程序

来源:互联网 发布:三盛网络 编辑:程序博客网 时间:2024/05/01 18:39

【更新】

2012-09-14,更新示例脚本,添加可选择的创建桌面快捷方式

【问题描述】Qt程序如何打包?最简单的方法是找到对应的动态链接库,然后连同程序一起打成压缩包发布(打包QT编写的应用程序)。另一种方法是编译为静态文件,作为绿色软件发布,网上介绍了相关的编译方法,比较复杂。一种方式是Windows如何打包Qt程序提供的方式。这几种方式效果不是很令人满意。还有一种就是利用InstallShield打包,该软件比较复杂,可以仔细研究。本文以<文件加密专家>程序为例,介绍一种利用Inno Setup打包Qt程序的方法。

 

【准备工作】

1 下载安装Inno Setup

http://www.jrsoftware.org/isdl.php

*网上有汉化版,可以自己找找

2 准备打包程序图标setup.ico,就是打包好的程序,你所想显示的图标,注意为ico格式。介绍一个在线制作ico的网站,能将其他图片格式快速转变为ico格式:

http://www.ico.la/

 

【操作步骤】

1 准备待打包的程序和动态链接库,注意动态链接库的存放位置。本例目录结构为:

图1 目录结构

源目录

F:\FileEncryExpert v1.2

源程序

F:\FileEncryExpert v1.2\文件加密专家.exe

动态链接库F:\FileEncryExpert v1.2\libgcc_s_dw2-1.dll

F:\FileEncryExpert v1.2\mingwm10.dll

F:\FileEncryExpert v1.2\QtCore4.dll

F:\FileEncryExpert v1.2\QtGui4.dll

F:\FileEncryExpert v1.2\QtSql4.dll

 在Qt安装目录中查找,E:\QtSDK\Desktop\Qt\4.8.0\mingw\bin

 

如果用到png库、sql库之类的动态链接库,要放入plugins目录

F:\FileEncryExpert v1.2\plugins\sqldrivers\qsqlite4.dll

F:\FileEncryExpert v1.2\plugins\sqldrivers\qsqlodbc4.dll

在Qt安装目录查找,E:\QtSDK\Desktop\Qt\4.8.0\mingw\plugins,注意子目录必须和安装目录中的子目录一致,如本例/plugins/下的sql相关子目录必须以sqldrivers命名

程序加载这些库的方法是,在main函数中,添加:

qApp->addLibraryPath(qApp->applicationDirPath () + "/plugins");

 

自述文件

F:\FileEncryExpert v1.2\Readme.txt

*这个存放自述信息

 

版权文件

F:\FileEncryExpert v1.2\license.txt

*这个存放软件运行需要的协议

 

2 打开Inno Setup,建立一个空的工程。工程建立后,实际上是一个文本编辑器。所以Inno setup体积很小。

 

3 参考帮助文档,编写脚本。

 

4 执行 构建->编译,若脚本文件无误,则会在脚本指定的输出目录。产生你打包好的安装程序。

 *本例输出目录为F:\ouput,目录需要自己建立。

 

【示例脚本】

; 脚本由 Inno Setup 脚本向导 生成!; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档![Setup]AppName=文件加密专家AppVersion=1.2.2AppPublisher=xxxx.AppPublisherURL=http://www.company.cn/AppSupportURL=http://www.company.cn/AppUpdatesURL=http://www.company.cn/AppCopyright=Copyright (C) 2012-2015 xxxx.VersionInfoVersion=1.2.0.2DefaultDirName={pf}\文件加密专家DefaultGroupName=文件加密专家SetupIconFile=setup.icoUninstallDisplayIcon={app}\文件加密专家.exeCompression=lzma2SolidCompression=yesShowLanguageDialog=yesSourceDir=F:\FileEncryExpert v1.2OutputDir=F:\ouputOutputBaseFilename=setupLicenseFile=license.txt[Languages]Name: "chs"; MessagesFile: "compiler:Default.isl" Name: "en"; MessagesFile: "compiler:English.isl"[Files]Source: "文件加密专家.exe"; DestDir: "{app}"Source: "QtGui4.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "QtCore4.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "QtSql4.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "mingwm10.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "libgcc_s_dw2-1.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: ".\plugins\sqldrivers\qsqlite4.dll"; DestDir: "{app}\plugins\sqldrivers"; Flags: onlyifdoesntexistSource: ".\plugins\sqldrivers\qsqlodbc4.dll"; DestDir: "{app}\plugins\sqldrivers"; Flags: onlyifdoesntexistSource: "Readme.txt"; DestDir: "{app}";[Icons]Name: "{group}\文件加密专家"; Filename: "{app}\文件加密专家.exe"Name: "{group}\卸载文件加密专家"; Filename: "{app}\unins000.exe"Name: "{userdesktop}\文件加密专家";Filename: "{app}\文件加密专家.exe"; WorkingDir: "{app}"; Tasks: desktopicon[Tasks]Name: desktopicon; Description: "创建桌面快捷方式(&D)"; GroupDescription: "添加快捷方式:" [registry];本段处理程序在注册表中的键值Root:HKLM;Subkey:SOFTWARE\FileEncryExpert;ValueType: string; ValueName:FileEncryExpert;ValueData:{app}\文件加密专家.exe;Flags: uninsdeletevalueRoot:HKLM;Subkey:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FileEncryExpert;ValueType: string; ValueName:UninstallString;ValueData:{app}\unins000.exe;Flags: uninsdeletevalue[Run]Filename: "{app}\Readme.txt"; Description: "查看自述文件"; Flags: postinstall shellexec skipifsilentFilename: "{app}\文件加密专家.exe"; Description: "运行应用程序"; Flags: postinstall nowait skipifsilent unchecked [Code]function InitializeSetup(): boolean;var  ResultStr: String;  ResultCode: Integer;begin  if RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FileEncryExpert', 'UninstallString', ResultStr) then    begin      ResultStr := RemoveQuotes(ResultStr);      Exec(ResultStr, '/silent', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);    end;    result := true;end;


【脚本解析】

1 setup段

AppName=文件加密专家AppVersion=1.2.2AppPublisher=xxxx.AppPublisherURL=http://www.company.cn/AppSupportURL=http://www.company.cn/AppUpdatesURL=http://www.company.cn/AppCopyright=Copyright (C) 2012-2015 xxxx.VersionInfoVersion=1.2.0.2

这一部分指定了应用名称、版本信息和版权信息等。

 

DefaultDirName={pf}\文件加密专家

程序安装的默认目录,即C:\Program Files\文件加密专家

 

DefaultGroupName=文件加密专家

默认的组名,所谓的组,就是 开始菜单->程序 中出现的组,组下可以有多个程序。

 

SetupIconFile=setup.ico

你自己准备的安装程序图标文件

 

UninstallDisplayIcon={app}\文件加密专家.exe

指定卸载程序的显示图标

 

Compression=lzma2SolidCompression=yes

设置打包的压缩算法

 

ShowLanguageDialog=yes

设置在安装过程中,显示语言选择提示,与

[Languages]Name: "chs"; MessagesFile: "compiler:Default.isl" Name: "en"; MessagesFile: "compiler:English.isl"

配合使用。

 

SourceDir=F:\FileEncryExpert v1.2OutputDir=F:\ouput

源文件的目录和安装程序输出目录

 

OutputBaseFilename=setup

安装程序的名称

 

LicenseFile=license.txt

指定协议文件,安装时,同意了协议,才能继续安装。

 

2 Files字段

Source: "文件加密专家.exe"; DestDir: "{app}"

指定程序的路径

 

Source: "QtGui4.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "QtCore4.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "QtSql4.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "mingwm10.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: "libgcc_s_dw2-1.dll"; DestDir: "{app}"; Flags: onlyifdoesntexistSource: ".\plugins\sqldrivers\qsqlite4.dll"; DestDir: "{app}\plugins\sqldrivers"; Flags: onlyifdoesntexistSource: ".\plugins\sqldrivers\qsqlodbc4.dll"; DestDir: "{app}\plugins\sqldrivers"; Flags: onlyifdoesntexist

指定动态链接库路径

 

Source: "Readme.txt"; DestDir: "{app}";

指定自述文件路径。


3 Icons字段

Name: "{group}\文件加密专家"; Filename: "{app}\文件加密专家.exe"Name: "{group}\卸载文件加密专家"; Filename: "{app}\unins000.exe"

指定 开始菜单->程序->文件加密专家 组下的程序

 

Name: "{userdesktop}\文件加密专家";Filename: "{app}\文件加密专家.exe"; WorkingDir: "{app}"; Tasks: desktopicon

添加桌面快捷方式

 

4 Tasks字段(2012-09-14)

Name: desktopicon; Description: "创建桌面快捷方式(&D)"; GroupDescription: "添加快捷方式:"

选择是否创建桌面快捷方式

 

5 registry字段

Root:HKLM;Subkey:SOFTWARE\FileEncryExpert;ValueType: string; ValueName:FileEncryExpert;ValueData:{app}\文件加密专家.exe;Flags: uninsdeletevalueRoot:HKLM;Subkey:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FileEncryExpert;ValueType: string; ValueName:UninstallString;ValueData:{app}\unins000.exe;Flags: uninsdeletevalue

指定注册表信息

 

6 Run字段

Filename: "{app}\Readme.txt"; Description: "查看自述文件"; Flags: postinstall shellexec skipifsilentFilename: "{app}\文件加密专家.exe"; Description: "运行应用程序"; Flags: postinstall nowait skipifsilent unchecked

添加了这一部分,安装完成后,提供查看自述文件和运行应用程序的选项

 

7 Code字段

[Code]function InitializeSetup(): boolean;var  ResultStr: String;  ResultCode: Integer;begin  if RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FileEncryExpert', 'UninstallString', ResultStr) then    begin      ResultStr := RemoveQuotes(ResultStr);      Exec(ResultStr, '/silent', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);    end;    result := true;end;

这个字段的功能是在安装之前,是否有软件的卸载注册表信息。如果有该信息,则说明安装了该软件。则在选择安装语言后,调用卸载程序,对程序进行卸载后,再安装新的程序。

 

【打包效果】

效果如图2所示:

图2 打包好的安装程序

 

 

 【学习参考】

1 http://379910987.blog.163.com/blog/static/33523797201102382952519/

 

 

 

转载请标明出处,仅供学习交流,勿用于商业目的

Copyright @ http://blog.csdn.net/tandesir


 

 

原创粉丝点击