System Information Example
系统信息示例
Using the systemInfo API in a component script to check operating system version and bitness.
在组件脚本中使用systemInfo API检查操作系统版本和位。
The System Information Example demonstrates how to use the systemInfo API to detect details about the target operating system.
系统信息示例演示如何使用systemInfo API来检测有关目标操作系统的详细信息。
The example installer consists of three packages: root
, root.i386
and root.x86_64
. The root.i386
and root.x86_64
packages are assumed to contain binaries specific to the architecture. The root
package contains logic to check for minimum operating system version. It also hides either the root.i386
or root.x86_64
package, based on the operating system architecture.
示例安装程序由三个包组成:root、root.i386和root.x86_64。假设root.i386和root.x86_64包包含特定于该体系结构的二进制文件。根包包含检查最低操作系统版本的逻辑。它还根据操作系统架构隐藏了root.i386或root.x86_64包。
The logic to detect the operating system features is scripted in the root
's installscript.qs
file.
检测操作系统功能的逻辑在root的installscript.qs文件中编写。
Helper Functions
帮助函数
The installscript.qs
file first declares two helper functions: cancelInstaller()
and majorVersion()
.
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()
sets all except the last installer page to invisible, and shows an error message on the last one. This is a technique also demonstrated in the componenterror and quitinstaller examples.
cancelInstaller()将除最后一个安装程序页面之外的所有页面设置为不可见,并在最后一个页面上显示错误消息。这也是在componenterror和quitinstaller示例中演示的一种技术。
function majorVersion(str)
{
return parseInt(str.split(".", 1));
}
majorVersion()
takes a string of the format <number>.<number>.<number>.[...]
. It uses the built-in JavaScript functions string.split()
and parseInt()
to return the first <number>
as an integer.
majorVersion()接受<number>格式的字符串 <number>.<number>.<number>.[...]
它使用内置的JavaScript函数string.split()和parseInt()将第一个<number>作为整数返回。
Checking the Operating System Type
检查操作系统类型
The actual checks are executed as soon as the package is loaded, in the Component
constructor function.
实际检查在加载包后立即在Component构造函数中执行。
function Component()
{
The function uses the built-in systemInfo.kernelType, systemInfo.kernelVersion, systemInfo::productType, and systemInfo.productVersion properties to check the minimum system requirements.
该函数使用内置的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;
}
The script uses systemInfo.productType to differentiate between Windows, macOS, and individual Linux distributions.
该脚本使用systemInfo.productType来区分Windows、macOS和单个Linux发行版。
For macOS and Windows, the script checks the operating system kernel version. For a mapping of kernel to operating system versions, see Darwin and Windows NT.
对于macOS和Windows,该脚本检查操作系统内核版本。有关内核到操作系统版本的映射,请参阅Darwin和Windows NT。
In the case of Linux, it checks the distribution name and version. If it does not match the specific distribution and version the binaries are presumably built for, the installer shows a warning in a modal dialog, but allows installation.
对于Linux,它会检查发行版名称和版本。如果它与二进制文件可能构建的特定发行版和版本不匹配,安装程序将在模式对话框中显示警告,但允许安装。
If the Windows or macOS version is too old, though, the script calls the cancelInstaller()
helper function to prevent an actual installation:
但是,如果Windows或macOS版本太旧,脚本会调用cancelInstaller()辅助函数来阻止实际安装:
if (!validOs) {
cancelInstaller("Installation on " + systemInfo.prettyProductName + " is not supported");
return;
}
Checking the Operating System Architecture
检查操作系统架构
The next section demonstrates the use of systemInfo.currentCpuArchitecture to choose the appropriate sub-package for a particular architecture:
下一节将演示如何使用systemInfo.currentPuArchitecture为特定体系结构选择合适的子包:
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");
}
}
Depending on the operating system architecture, either the package root.i386
or root.x86_64
is marked Virtual
, hiding it from the user. For the package that matches the local architecture, the Default
property is set to check the package by default.
根据操作系统体系结构,root.i386或root.x86_64包被标记为Virtual,对用户隐藏。对于与本地体系结构匹配的包,默认情况下将Default属性设置为检查包。
Generating the Example Installer
生成示例安装程序
To create the example installer, switch to the example source directory on the command line and enter the following command:
要创建示例安装程序,请在命令行上切换到示例源代码目录,然后输入以下命令:
- On Windows:
..\..\bin\binarycreator.exe -c config\config.xml -p packages installer.exe
- On Linux or macOS:
../../bin/binarycreator -c config/config.xml -p packages installer
This creates the installer to the current directory.
这将创建当前目录的安装程序。
Files:
- systeminfo/config/config.xml
- systeminfo/packages/root.i386/meta/package.xml
- systeminfo/packages/root.x86_64/meta/package.xml
- systeminfo/packages/root/meta/installscript.qs
- systeminfo/packages/root/meta/package.xml
- systeminfo/systeminfo.pro
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. The Qt Company, Qt and their respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.