Register File Extension Example

Using a component script to register a filename extension on 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.

Configuring the Example Installer

The installer configuration file, config.xml, in the config directory specifies the text and default values used in the installer:

  • The <Name> element specifies the application name that is added to the page name and introduction text.
  • The <Version> element specifies the application version number.
  • The <Title> element specifies the installer name displayed on the title bar.
  • The <Publisher> element specifies the publisher of the software (as shown in the Windows Control Panel, for example).
  • The <StartMenuDir> element specifies the name of the default program group for the product in the Windows Start menu.
  • The <TargetDir> element specifies that the default target directory is located in the IfwExamples directory in the home directory of the current user (because the predefined variable@HomeDir@ is used as a part of the value). For more information, see Predefined Variables.
<?xml version="1.0" encoding="UTF-8"?>
    <Name>Register File Extension Example</Name>
    <Title>Register File Extension Example</Title>
    <StartMenuDir>Qt IFW Examples</StartMenuDir>

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:

  • The <DisplayName> element specifies the human-readable name of the component.
  • The <Description> element specifies the human-readable description of the component.
  • The <Version> element specifies the version number of the component.
  • The <ReleaseDate> element specifies the date when this component version was released.
  • The <Default> element is set to true to preselect the component in the installer.
  • The <Script> element specifies the file name of the JavaScript file that is loaded to perform operations.
  • The <UserInterfaces> element specifies the file names of the installer pages (.ui files) to use.
<?xml version="1.0" encoding="UTF-8"?>
    <DisplayName>Register a file extension</DisplayName>
    <Description>Register a randomly generated file extension to open with notepad.exe</Description>

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:

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 = 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:

    installer.finishButtonClicked.connect(this, openRegisteredFileIfChecked);

We call the openRegisteredFileIfChecked function to check that the file type was successfully registered:

openRegisteredFileIfChecked = function()
    if (!component.installed)

    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:

    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:


    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";
                               notepadPath + " '%1'",
                               "QInstaller Framework example file type",
                               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

The installer is created in the current directory.

After running the installer, double-click the installed file, registeredfile.<extension>, to open it in Notepad.


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