Register File Extension Example
注册文件扩展名示例
Using a component script to register a filename extension on Windows.
使用组件脚本在Windows上注册文件扩展名。
Register File Extension illustrates how to register a file type on Windows. We use the component.addOperation()
function with the RegisterFileType
operation to register a randomly generated file type to be opened with Notepad.
注册文件扩展名说明了如何在Windows上注册文件类型。我们使用component.addOperation()函数和RegisterFileType操作来注册一个随机生成的文件类型,以便用记事本打开。
Configuring the Example Installer
配置示例安装程序
The installer configuration file, config.xml, in the config
directory specifies the text and default values used in the installer:
config目录中的安装程序配置文件config.xml指定了安装程序中使用的文本和默认值:
- The
<Name>
element sets the application name and adds it to the page name and introduction text. - <Name>元素设置应用程序名称并将其添加到页面名称和介绍文本中。
- The
<Version>
element sets the application version number. - <Version>元素设置应用程序版本号。
- The
<Title>
element sets the installer name and displays it on the title bar. - <Title>元素设置安装程序名称并将其显示在标题栏上。
- The
<Publisher>
element sets the publisher of the software (as shown in the Windows Control Panel, for example). - <Publisher>元素设置软件的发布者(例如,如Windows控制面板所示)。
- The
<StartMenuDir>
element sets the name of the default program group for the product in the Windows Start menu. - <StartMenuDir>元素在Windows“开始”菜单中设置产品的默认程序组的名称。
- The
<TargetDir>
element sets the default target directory location to be within theIfwExamples
directory in the home directory of the current user (because it uses the pre-existing variable,
@HomeDir@, as part of the value). For more information, see Predefined Variables. - <TargetDir>元素将默认目标目录位置设置为当前用户主目录中的IfwExamples目录内(因为它使用预先存在的变量@HomeDir@作为值的一部分)。有关详细信息,请参见预定义变量。
<?xml version="1.0" encoding="UTF-8"?>
<Installer>
<Name>Register File Extension Example</Name>
<Version>1.0.0</Version>
<Title>Register File Extension Example</Title>
<Publisher>Qt-Project</Publisher>
<StartMenuDir>Qt IFW Examples</StartMenuDir>
<TargetDir>@HomeDir@/IfwExamples/registerfileextension</TargetDir>
</Installer>
Creating the Example Package Information File
创建示例包信息文件
The installer package information file, package.xml, in the meta
directory specifies the components that are available for installation:
meta
目录中的安装程序包信息文件package.xml指定了可用于安装的组件:
- The
<DisplayName>
element sets the human-readable name of the component. - <DisplayName>元素设置组件的人类可读名称。
- The
<Description>
element sets the human-readable description of the component. - <Description>元素设置组件的人类可读描述。
- The
<Version>
element sets the version number of the component. - <Version>元素设置组件的版本号。
- The
<ReleaseDate>
element sets the date of release for this component version. - <ReleaseDate>元素设置此组件版本的发布日期。
- The
<Default>
element is set totrue
to preselect the component in the installer. - 将<Default>元素设置为true,以在安装程序中预选组件。
- The
<Script>
element specifies the file name of the JavaScript file that is loaded to perform operations. - <Script>元素指定加载以执行操作的JavaScript文件的文件名。
- The
<UserInterfaces>
element specifies the file names of the installer pages (.ui files) to use. - <UserInterfaces>元素指定要使用的安装程序页面(.ui文件)的文件名。
<?xml version="1.0" encoding="UTF-8"?>
<Package>
<DisplayName>Register a file extension</DisplayName>
<Description>Register a randomly generated file extension to open with notepad.exe</Description>
<Version>1.0.0-1</Version>
<ReleaseDate>2013-01-01</ReleaseDate>
<Default>true</Default>
<Script>installscript.qs</Script>
<UserInterfaces>
<UserInterface>registerfilecheckboxform.ui</UserInterface>
<UserInterface>openfilecheckboxform.ui</UserInterface>
</UserInterfaces>
</Package>
Registering File Extensions
注册文件扩展名
In installscript.qs, we call the Component()
function to connect to the addRegisterFileCheckBox
signal as soon as all components have been loaded:
在installscript.qs中,我们调用Component()函数,以便在加载所有组件后立即连接到addRegisterFileCheckBox信号:
function Component()
{
component.loaded.connect(this, addRegisterFileCheckBox);
We use the addRegisterFileCheckBox()
function to display a check box for registering the generated file type on the last page of the installer. We hide the page when updating and uninstalling:
我们使用addRegisterFileCheckBox()函数在安装程序的最后一页显示一个复选框,用于注册生成的文件类型。我们在更新和卸载时隐藏页面:
addRegisterFileCheckBox = function()
if (installer.isInstaller()) {
if (installer.addWizardPageItem(component, "RegisterFileCheckBoxForm", QInstaller.TargetDirectory)) {
component.userInterface("RegisterFileCheckBoxForm").RegisterFileCheckBox.text =
component.userInterface("RegisterFileCheckBoxForm").RegisterFileCheckBox.text + component.unusualFileType;
}
}
}
We connect to the openRegisteredFileIfChecked
signal when end users select Finish on the last installer page:
当最终用户在最后一个安装程序页面上选择Finish时,我们连接到openRegisteredFileIfChecked信号:
installer.finishButtonClicked.connect(this, openRegisteredFileIfChecked);
We call the openRegisteredFileIfChecked
function to check that the file type was successfully registered:
我们调用openRegisteredFileIfChecked函数来检查文件类型是否已成功注册:
openRegisteredFileIfChecked = function()
{
if (!component.installed)
return;
if (installer.value("os") == "win" && installer.isInstaller() && installer.status == QInstaller.Success) {
var isOpenRegisteredFileChecked = component.userInterface("OpenFileCheckBoxForm").OpenRegisteredFileCheckBox.checked;
if (isOpenRegisteredFileChecked) {
QDesktopServices.openUrl("file:///" + component.fileWithRegisteredType);
}
}
}
We bind the unusualFileType
variable to the generateUnusualFileType()
function that randomly generates a file type with the specified length:
我们将unusualFileType变量绑定到generateUnusualFileTypes()函数,该函数随机生成具有指定长度的文件类型:
component.unusualFileType = generateUnusualFileType(5)
}
generateUnusualFileType = function(length)
{
var randomString = "";
var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++)
randomString += possible.charAt(Math.floor(Math.random() * possible.length));
return randomString;
}
We use the RegisterFileType
operation to create a file of the specified type and to specify the application to open the file with:
我们使用RegisterFileType操作创建指定类型的文件,并使用以下命令指定打开该文件的应用程序:
component.createOperations();
if (component.userInterface("RegisterFileCheckBoxForm")) {
var isRegisterFileChecked = component.userInterface("RegisterFileCheckBoxForm").RegisterFileCheckBox.checked;
}
if (installer.value("os") === "win") {
var iconId = 0;
var notepadPath = installer.environmentVariable("SystemRoot") + "\\notepad.exe";
component.addOperation("RegisterFileType",
component.unusualFileType,
notepadPath + " '%1'",
"QInstaller Framework example file type",
"text/plain",
notepadPath + "," + iconId,
"ProgId=QtProject.QtInstallerFramework." + component.unusualFileType);
}
component.fileWithRegisteredType = installer.value("TargetDir") + "/registeredfile." + component.unusualFileType
component.addOperation("Move", "@TargetDir@/registeredfile", component.fileWithRegisteredType);
}
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.
这将创建当前目录的安装程序。
After running the installer, double-click the installed file, registeredfile.<extension>
, to open it in Notepad.
运行安装程序后,双击已安装的文件registeredfile<扩展>,在记事本中打开它。
Files:
- registerfileextension/config/config.xml
- registerfileextension/packages/org.qtproject.ifw.example.registerfileextension/meta/installscript.qs
- registerfileextension/packages/org.qtproject.ifw.example.registerfileextension/meta/openfilecheckboxform.ui
- registerfileextension/packages/org.qtproject.ifw.example.registerfileextension/meta/package.xml
- registerfileextension/packages/org.qtproject.ifw.example.registerfileextension/meta/registerfilecheckboxform.ui
- registerfileextension/registerfileextension.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.