系统信息示例
在组件脚本中使用 systemInfo API 检查操作系统版本和位数。
系统信息示例演示了如何使用 systemInfo API 来检测目标操作系统的详细信息。
示例安装程序由三个包组成:root、root.i386 和 root.x86_64。假定 root.i386 和 root.x86_64 包包含特定于架构的二进制文件。root 包包含检查最小操作系统版本的逻辑。它还根据操作系统架构隐藏 root.i386 或 root.x86_64 包。
检测操作系统功能的逻辑脚本在 root 的 installscript.qs 文件中。
辅助函数
installscript.qs 文件首先声明了两个辅助函数:cancelInstaller() 和 majorVersion()。
function cancelInstaller(message)
{
installer.setDefaultPageVisible(QInstaller.Introduction, false);
installer.setDefaultPageVisible(QInstaller.TargetDirectory, false);
installer.setDefaultPageVisible(QInstaller.ComponentSelection, false);
installer.setDefaultPageVisible(QInstaller.ReadyForInstallation, false);
installer.setDefaultPageVisible(QInstaller.StartMenuSelection, false);
installer.setDefaultPageVisible(QInstaller.PerformInstallation, false);
installer.setDefaultPageVisible(QInstaller.LicenseCheck, false);
var abortText = "<font color='red'>" + message +"</font>";
installer.setValue("FinishedText", abortText);
}cancelInstaller() 将除了最后一页外的所有安装者页面设置为不可见,并在最后一页显示错误消息。这是一种在 componenterror 和 quitinstaller 示例中也演示过的技术。
function majorVersion(str)
{
return parseInt(str.split(".", 1));
}majorVersion() 接受一个格式为 <number>.<number>.<number>.[...] 的字符串。它使用内置的 JavaScript 函数 string.split() 和 parseInt() 来返回作为一个整数的第一个 <number>。
检查操作系统类型
实际的检查在包一旦被加载,就在 Component 构造函数中立即执行。
function Component()
{该函数使用内置的 systemInfo.kernelType、systemInfo.kernelVersion、systemInfo::productType 和 systemInfo.productVersion 属性来检查最小系统要求。
var validOs = false;
if (systemInfo.kernelType === "winnt") {
if (majorVersion(systemInfo.kernelVersion) >= 6)
validOs = true;
} else if (systemInfo.kernelType === "darwin") {
if (majorVersion(systemInfo.kernelVersion) >= 11)
validOs = true;
} else {
if (systemInfo.productType !== "opensuse"
|| systemInfo.productVersion !== "13.2") {
QMessageBox["warning"]("os.warning", "Installer",
"Note that the binaries are only tested on OpenSUSE 13.2.",
QMessageBox.Ok);
}
validOs = true;
}脚本使用 systemInfo.productType 来区分 Windows、macOS 和个别 Linux 发行版。
对于 macOS 和 Windows,脚本检查操作系统内核版本。请参阅 Darwin 和 Windows NT 了解内核到操作系统版本的映射。
对于 Linux,它检查发行版的名称和版本。如果不匹配为二进制文件构建的特定发行版和版本,安装程序将在模态对话框中显示警告,但允许安装。
但是,如果 Windows 或 macOS 版本太旧,脚本将调用辅助函数 cancelInstaller() 以防止实际安装。
if (!validOs) {
cancelInstaller("Installation on " + systemInfo.prettyProductName + " is not supported");
return;
}检查操作系统架构
下一节将演示使用 systemInfo.currentCpuArchitecture 来选择特定架构的合适子包。
installer.componentByName("root.i386").setValue("Virtual", "true");
installer.componentByName("root.x86_64").setValue("Virtual", "true");
if ( systemInfo.currentCpuArchitecture === "i386") {
installer.componentByName("root.i386").setValue("Virtual", "false");
installer.componentByName("root.i386").setValue("Default", "true");
}
if ( systemInfo.currentCpuArchitecture === "x86_64") {
installer.componentByName("root.x86_64").setValue("Virtual", "false");
installer.componentByName("root.x86_64").setValue("Default", "true");
}
}根据操作系统架构,包root.i386或root.x86_64之一的标记为虚拟的,将其隐藏给用户。对于匹配本地架构的包,默认属性被设置为默认检查该包。
生成示例安装程序
要创建示例安装程序,请在命令行中切换到示例源目录,并输入以下命令
- 在Windows上
..\..\bin\binarycreator.exe -c config\config.xml -p packages installer.exe
- 在Linux或macOS上
../../bin/binarycreator -c config/config.xml -p packages installer
这将在当前目录下创建安装程序。
文件
©2021 Qt公司有限公司版权所有。本文件中的文档贡献属于其各自所有者的版权。本文件提供的文档受自由软件基金会发布的GNU自由文档许可证版本1.3条款的约束。Qt公司和Qt及其相应标志是芬兰和/或其他国家的商标,所有其他商标均为其各自所有者的财产。