qtframeworksinstallationqt-installer

Qt Installer Framework - Prevent installation in non-empty folder


I use Qt Installer Framework.

I want to know how to prevent installation in a non-empty folder?

In addition, how can I make the uninstaller remove only the files and folders it has installed, and not all the contents of its folder?

Thank you in advance


Solution

  • Due to the Qt Documentation you have access to the QDesktopServices inside the Scripting API, which contains the function findFiles(string path,string pattern), which should return none in your case, where path is your target directory for installation.

    In the TargetDirectory Installer Installer Page you have access to the set TargetDirectory via Installer Page, which can be connected to if there is some edit.

    It took me some hard time to figure out the following solution, which I have to admit is not the way I wanted to have it beforehand.

    First of all you need a control script, that needs to be set inside your config.xml. It is called controlScript.js and resides in the same directory as your config.xml.

    config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Installer>
        <Name>Your application</Name>
        <Version>1.0.0</Version>
        <Title>Your application Installer</Title>
        <Publisher>Your vendor</Publisher>
        <StartMenuDir>Super App</StartMenuDir>
        <TargetDir>@HomeDir@/InstallationDirectory</TargetDir>
        <ControlScript>controlScript.js</ControlScript>
    </Installer>
    

    Now, the only solution that I brought to work is the following:

    controlScript.js

    function Controller()
    {
        console.log("Control script");
    }
    
    Controller.prototype.ComponentSelectionPageCallback = function() {
        var widget = gui.currentPageWidget();
        widget.title="Select empty directory!";
        var targetDirWidget = gui.pageById(QInstaller.TargetDirectory);
    
        var targetDir=targetDirWidget.TargetDirectoryLineEdit.text;
    
        var files=QDesktopServices.findFiles(targetDir, "*.*");
        if (files.length!=0) {
            var result = QMessageBox.warning("quit.question", "You selected a non-empty directory!", targetDir,QMessageBox.Ok);
            gui.clickButton(buttons.BackButton);        
        }    
    
    }
    

    Basically, the user enters a target directory and presses the BackButton. In case the target directory is non-empty a message box is displayed and the BackButton in the Component Selection is pressed.

    My desired solution was beforehand to deactivate the NextButton in the TargetDirectory Page, but all my attempts were futile. Even though the following disabling of the NextButton should work, as shown in this answer.