Register File Extension Example

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.
<?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>
- The
<Default>element is set totrueto 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"?>
<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:
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()) {
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)
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:
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:
component.createOperations();
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);
}
After running the installer, double-click the installed file, registeredfile.<extension>, to open it in Notepad.
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