End to End GUI Development with Qt5
上QQ阅读APP看书,第一时间看更新

Qt Installer framework

Let's edit our Qt installation and grab the Qt Installer framework.

Launch the MaintenanceTool application from your Qt installation directory, and you will be presented with a wizard virtually identical to the one we saw when we first installed Qt. To add Qt Installer Framework to your existing installation, follow these steps:

  1. Either log in to your Qt Account or Skip
  2. Select Add or remove components and click on Next
  3. On the Select Components dialog, check Tools > Qt Installer Framework 3.0 and click on Next
  4. Begin the installation by clicking on Update

Once complete, you can find the installed tools in Qt/Tools/QtInstallerFramework/3.0.

You can add further modules, kits, and such in exactly the same way. Any components you already have installed will be unaffected unless you actively deselect them.

The Qt Installer Framework requires two specific directories to be present: config and packages. Config is a singular piece of configuration that describes the installer as a whole, whereas you can bundle multiple packages (or components) together in the same installation package. Each component has its own subdirectory within the packages folder, with a data folder containing all the items to be installed for that component and a meta folder where configuration data for the package is held.

In our case, although we have two projects (cm-lib and cm-ui), it makes no sense to distribute one without the other, so we will aggregate the files together into one package. A common naming convention for packages is com.<publisher>.<component>, so we’ll name ours com.packtpub.cm. We already created the required data folder in the previous section (yay for forward planning!) and windeployqt stuffed it full of files for us.

There is no required naming convention here, so feel free to name the package something else if you wish. If we wanted to bundle an additional, optional component with our application, we would do so by simply creating an additional package folder (for example, com.packtpub.amazingcomponent) containing the relevant data and meta files, including a separate package.xml to configure that component.

Create any missing folders so that you end up with the following folder structure inside cm/installer/windows:

To compliment these folders, we also need to provide two XML configuration files.

Create config.xml in the config subfolder:

<?xml version="1.0" encoding="UTF-8"?>
<Installer>
    <Name>Client Management</Name>
    <Version>1.0.0</Version>
    <Title>Client Management Application Installer</Title>
    <Publisher>Packt Software Publishing</Publisher>
    <StartMenuDir>Client Management</StartMenuDir>
    <TargetDir>@HomeDir@/ClientManagement</TargetDir>
</Installer>

This configuration file customizes the behavior of the installer. The properties we have specified here are as follows:

You will note strange @ symbols in the TargetDir property, and they define a predefined variable HomeDir that allows us to dynamically obtain a path to the end user’s home directory. You can also access the values of other properties in the same way, for example, @ProductName@ will return “Client Management”. Further information is available at http://doc.qt.io/qtinstallerframework/scripting.html#predefined-variables.

Next, create package.xml in the packages/com.packtpub.cm/meta subfolder:

<?xml version="1.0" encoding="UTF-8"?>
<Package>
    <DisplayName>Client Management application</DisplayName>
    <Description>Install the Client Management application.</Description>
    <Version>1.0.0</Version>
    <ReleaseDate>2017-10-30</ReleaseDate>
    <Licenses>
        <License name="Fictional Training License Agreement" file="license.txt" />
    </Licenses>
    <Default>true</Default>
</Package>

This file configures the com.packtpub.cm package (our Client Management application) with the following properties:

You will also need to create license.txt in the meta folder; the content doesn’t matter in this case as it’s just for demonstration, so write any old nonsense in there.

With all the binaries, dependencies, and configuration in place, we can now run the Qt Framework Installer in a command terminal to generate our installation package. First, change directory to the cm/installer/windows folder and then execute binarycreator:

$ <Qt Installation Path> Tools QtInstallerFramework 3.0 bin binarycreator.exe -c configconfig.xml -p packages ClientManagementInstaller.exe

The -c flag tells the tool where the config.xml file resides and -p where all the packages are. The final parameter is the name you want to give the resulting installer.

With our application neatly packaged up into a single installer file, ClientManagementInstaller.exe, we can now easily distribute it to our end users for installation.