Important: Please read the Qt Code of Conduct -

QtQuickControls FileDialog Automation

  • I'm trying to automate picking a file in QML in a QtQuick Controls FileDialog. How can I invoke FileDialog's accept with a specific fileUrl when the fileUrl property is read only?

    The current attempt involves calling filedialog.clearSelection, filedialog.addSelection and finally filedialog.accept. clearSelection and addSelection are not documented but can be found in source (Assuming Qt has used a DefaultFileDialog as this can be system dependent)

    However clearSelection seems to only work sporadically, having no affect if the same FileDialog has been used manually, hence the addSelection fails to set fileUrl.

    The following is a QML file (loaded as a basic project within QtCreator) demonstrating this. with a manual file dialog open button and an automatic one:

    import QtQuick 2.9
    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.4
    import QtQuick.Dialogs 1.3
    Window {
        visible: true;width: 200;height:200
        FileDialog {id: filedialog; onAccepted: console.log("File Dialog Accepted: ", fileUrl, fileUrls);}
        Row {
            Button {text: "manual"; onClicked:}
            Button {
                text: "auto_qml"
                onClicked: {
                    console.log("Current selection:", filedialog.fileUrl, filedialog.fileUrls)
                    console.log("cleared selection:", filedialog.fileUrl, filedialog.fileUrls) // only clears selection if manual not used
                    let t = filedialog.addSelection("file:/home/user/tempfile.txt");
                    console.log("add selection success:", t) // a non existent file returns false, so file must exist

Log in to reply