Search code examples
qtqmlqfiledialog

FileDialog is fine is debug mode, but not as expected in release mode


A QML FileDialog to save a file works fine in debug mode.

Screenshot: debug mode: fine

The code is:

import QtQuick 2.5
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.1
import QtQuick.Controls.Styles 1.4
import Qt.labs.platform 1.0

Item {

    property string exportSceneName: "exported_scene"
    property url exportFolder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)

    signal  startExport()

    onStartExport: {
        runLogic()
    }

    function runLogic() {
        // ...
    }

    Button {
        onClicked: {
            fileDialog.open()
        }
    }
    
    FileDialog {
        id: fileDialog
        folder: exportFolder
        fileMode: FileDialog.SaveFile
        title: qsTr("Export Scene As STL")
        onAccepted: {
            exportFolder = folder
            var name = basename(file)
            exportSceneName = name
            startExport()
        }
    }

    function basename(str) {
        return (String(str).slice(String(str).lastIndexOf("/")+1))
    }

}

Release mode

Surprisingly, in release mode, the dialog is open-type rather than save-type:

Screenshot: release mode: bad

I have tried:

  • Change the QML imports versions
  • Clean the release build directory
  • Modify qtquickcontrols2.conf file

However, none of them worked! I have studied similar posts like this one, but suggestions didn't work. What else can I try? Thanks.

SOLUTION

Fixed by removing this import inside QML file:

import QtQuick.Dialogs 1.2

Solution

  • I'm going to guess that the issue is conflicting FileDialog definitions. Note that both imports QtQuick.Dialogs and Qt.labs.platform provide an object called FileDialog, but they do not use the same API. (There are several other objects like this in QML, and it's really annoying.) So it's probably trying to use one version of the dialog in debug mode, but for some reason choosing the other one in release mode.

    The solution is to first of all make sure you remove any imports that you're not actually using. Then if you still need both, then you can label the imports:

    import QtQuick.Dialogs 1.2 as QDiag
    import Qt.labs.platform 1.0 as QPlat
    

    Then when you create the FileDialog, you'll have to explicitly state which one you want to use.

    QDiag.FileDialog {
    }
    
    QPlat.FileDialog {
    }