Qt Installer Framework自定义安装程序

Customizing Installers

自定义安装程序

You can use scripting to customize installers by:

可以使用脚本通过以下方式自定义安装程序:

  • Adding Qt Installer Framework operations that are prepared by the scripts and performed by the installer.
  • 添加由脚本准备并由安装程序执行的Qt安装程序框架操作。
  • Adding new pages that you specify in the package.xml file and place in the packages directory.
  • 添加在package.xml文件中指定的新页面,并将其放置在packages目录中。
  • Modifying existing pages by inserting custom user interface elements into them as single widgets.
  • 通过将自定义用户界面元素作为单个widget插入现有页面来修改它们。
  • Adding language variants.
  • 添加语言变量。

You can use both component scripts and a control script to customize installers. A component script is associated with a particular component by specifying it in the Script element of the package.xml file of the component. The script is loaded when the component's metadata is fetched. For more information about component scripts, see Component Scripting.

​可以使用组件脚本和控制脚本来自定义安装程序。通过在组件的package.xml文件的Script元素中指定它,组件脚本与特定组件相关联。在获取组件的元数据时加载脚本。有关组件脚本的更多信息,请参阅组件脚本。

A control script is associated with the whole installer by specifying it in the ControlScript element of the config.xml file of the installer. Control scripts can be part of the installer resources or be passed on the command line. They can be used to modify the installer pages that are presented to users before components are loaded. Also, you can use them to modify pages in the uninstaller. For more information, see Controller Scripting.

​通过在安装程序的config.xml文件的ControlScript元素中指定控制脚本,控制脚本与整个安装程序相关联。控制脚本可以是安装程序资源的一部分,也可以在命令行上传递。它们可用于在加载组件之前修改呈现给用户的安装程序页面。此外,还可以使用它们来修改卸载程序中的页面。有关更多信息,请参阅控制器脚本。

For more information about the global JavaScript objects that can be used in component and control scripts, see Scripting API.

​有关可在组件和控制脚本中使用的全局JavaScript对象的更多信息,请参阅编写API脚本。

Adding Operations

添加操作

You can use component scripts to perform Qt Installer Framework operations during the installation process. Typically, operations manipulate files by moving, copying, or patching them. Use the QInstaller::Component::addOperation or QInstaller::Component::addElevatedOperation function to add operations. For more information, see Adding Operations to Components.

​可以在安装过程中使用组件脚本执行Qt Installer Framework操作。通常,操作通过移动、复制或修补文件来操纵文件。使用QInstaller::Component::addOperation或QInstaller:Component::addElevatedOperation函数添加操作。有关详细信息,请参见向组件添加操作。

In addition, you can implement methods to register custom installation operations in the installer by deriving KDUpdater::UpdateOperation. For more information, see Registering Custom Operations.

​此外,可以通过派生KDUpdater::UpdateOperation来实现在安装程序中注册自定义安装操作的方法。有关更多信息,请参阅注册自定义操作。

For a summary of available operations, see Operations.

​有关可用操作的摘要,请参阅操作。

Adding Pages

添加页面

A component can contain one or more user interface files, which are placed into the installer by a component or control script. The installer automatically loads all user interface files listed in the UserInterfaces element of the package.xml file.

一个组件可以包含一个或多个用户界面文件,这些文件由组件或控制脚本放置在安装程序中。安装程序会自动加载package.xml文件的UserInterfaces元素中列出的所有用户界面文件。

Using Component Scripts to Add Pages

使用组件脚本添加页面

To add a new page to the installer, use the installer::addWizardPage() method and specify the location of the new page. For example, the following code adds an instance of MyPage before the ready for installation page:

​要向安装程序添加新页面,请使用installer::addWizardPage()方法并指定新页面的位置。例如,以下代码在准备安装页面之前添加了MyPage的实例:

installer.addWizardPage( component, "MyPage", QInstaller.ReadyForInstallation );

You can use component scripts to access the loaded widgets by calling the component::userInterface() method with the class name of the widget, as illustrated by the following code snippet:

​可以使用组件脚本访问加载的widget,方法是使用widget的类名调用component::userInterface()方法,如以下代码片段所示:

component.userInterface( "MyPage" ).checkbox.checked = true;

You can also have a callback for the page that is added. To access it, use the object name set in the UI file (for example, "MyPage"). Then create the Dynamic${ObjectName}Callback function (for example, DynamicMyPageCallback):

还可以对添加的页面进行回调。要访问它,请使用UI文件中设置的对象名称(例如“MyPage”)。然后创建动态${ObjectName}Callback函数(例如,DynamicMyPageCallback):

Component.prototype.DynamicMyPageCallback = function()
{
    var page = gui.pageWidgetByObjectName("DynamicMyPage");
    page.myButton.click,
    page.myWidget.subWidget.setText("hello")
}

You can access widgets by using their object names that are set in the UI file. For example, myButton and myWidget are widget object names in the code above.

可以使用UI文件中设置的widget对象名称来访问它们。例如,myButton和myWidget是上面代码中的widget对象名称。

Adding Widgets

添加Widget

You can use component or control scripts to insert custom user interface elements into the installer as single widgets (such as a check box).

可以使用组件或控制脚本将自定义用户界面元素作为单个widget(如复选框)插入安装程序中。

To insert a single widget, use the installer::addWizardPageItem method. For example, the following code snippet adds an instance of MyWidget to the component selection page from within a script:

​要插入单个widget,请使用installer::addWizardPageItem方法。例如,以下代码片段将MyWidget的实例从脚本中添加到组件选择页面:

​installer.addWizardPageItem( component, "MyWidget", QInstaller.ComponentSelection );

Interacting with Installer Functionality

与安装程序功能交互

You can use control scripts to execute installer functions automatically in tests, for example. The following snippet illustrates how to automatically click the Next button on the target directory selection page:

例如,可以使用控制脚本在测试中自动执行安装程序函数。以下代码段说明了如何在目标目录选择页面上自动单击“下一步”按钮:

Controller.prototype.TargetDirectoryPageCallback = function()
{
    gui.clickButton(buttons.NextButton);
}

Translating Pages

翻译页面

The installer uses the Qt Translation system to support the translation of user-readable output to several languages. To provide end users with localized versions of strings contained in the component scripts and user interfaces, create QTranslator files that the installation system loads along with the component. The installer loads the translation file that matches the current system locale. For example, if the system locale is German, the de.qm file is loaded. In addition, a localized license_de.txt is shown instead of the default license.txt if it is found.

​安装程序使用Qt翻译系统支持将用户可读输出翻译成多种语言。为了向最终用户提供组件脚本和用户界面中包含的字符串的本地化版本,请创建安装系统随组件加载的QTranslator文件。安装程序加载与当前系统区域设置匹配的翻译文件。例如,如果系统区域设置为德语,则加载de.qm文件。此外,如果找到默认的license.txt,则显示本地化的license_de.txt,而不是默认的license.txt。

Translations need to be added to the package.xml file to be activated for a component:

需要将翻译添加到package.xml文件中,以便为组件激活:

<Translations>
    <Translation>de.qm</Translation>
</Translations>

Use the qsTranslate() function for literal text within scripts. Additionally, you can add the Component.prototype.retranslateUi method to the script. It is called when the language of the installer changes and the translation file is loaded.

对脚本中的文字文本使用qsTranslate()函数。此外,可以将Component.prototype.retranslateUi方法添加到脚本中。当安装程序的语言更改并且加载了翻译文件时,会调用它。

Note: The translation system can also be used to customize the UI. Use e.g. an en.ts file to replace any text in the installer with a custom English version.

注意:翻译系统也可用于自定义UI。例如,使用en.ts文件将安装程序中的任何文本替换为自定义英文版本。

Configuring and Overwriting Default Translations

配置和覆盖默认翻译

The installer has been localized into several languages. System language is used to define the loaded language. In case you want to define the used language for your installer, define the languages in config.xml using the <Translations> element. For example, using only German translations:

安装程序已本地化为多种语言。系统语言用于定义加载的语言。如果想为安装程序定义使用的语言,请使用<Translations>元素在config.xml中定义语言。例如,仅使用德语翻译:

<Translations>
    <Translation>ifw_de</Translation>
    <Translation>qt_de</Translation>
</Translations>

The default translations can be also overwritten. Write your own translation file and add it to a custom resource called :/translations_new. This custom resource can be added to the installer using binarycreator option -r. For more information, see Summary of binarycreator Parameters.

​默认翻译也可以被覆盖。编写自己的翻译文件并将其添加到名为:/translations_new的自定义资源中。可以使用binarycreator选项-r将此自定义资源添加到安装程序中。有关更多信息,请参阅binarycreatorParameters摘要。

If the translated language is not already part of the existing translations of Qt Installer Framework, you need to also include the Qt Base translation for that language in the resource file. For this you need to point to the qtbase_*.qm for your language from the location of QT_INSTALL_TRANSLATIONS and alias it in the resource file.

如果翻译的语言还不是Qt Installer Framework现有翻译的一部分,还需要在资源文件中包含该语言的Qt Base翻译。为此,需要从QT_INSTALL_TRANLATIONS的位置指向您的语言的qtbase_*.qm,并在资源文件中将其别名。

For example, for the Czech translation a custom translations_new.qrc should look like this:

例如,对于捷克语翻译,自定义translations_new.qrc应该如下所示:

<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/translations_new">
    <file>ifw_cs.qm</file>
    <file alias="qt_cs.qm">%QT_INSTALL_TRANSLATIONS%/qtbase_cs.qm</file>
</qresource>
</RCC>

The path for replacing QT_INSTALL_TRANSLATIONS can be retrieved from the output of qmake -query of your Qt installation.

替换QT_INSTALL_TRANLATIONS的路径可以从QT安装的qmake-query的输出中检索。

Promoting UpdatesQt Installer Framework Examples

© 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

꧁白杨树下꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值