Important: Please read the Qt Code of Conduct -

How to use dynamic path to read file on FileDialog open

  • Hi All,

    I have a menubutton in qml which open FileDialog on clicked , i need to set path to default folder which i have created as import :
    Sample code as fallows

    MenuButton {
    label: qsTr("Import")

                imageSource: "qrc:/images/abc.svg"
                onMenuButtonClicked: //Open file browser to choose file

    title: "Please choose a file"
    folder: "file:///C:/Users/Documents/gui/Import" //need to set dynamic path

        onAccepted: {
            selectedFilePath  = folder

    tried setting folder: "file:///" + applicationDirPath + /gui/Import" did not work

    Thanks :)

  • Moderators

    Please use code tags around code, it's easier to read.

    tried setting folder: "file:///" + applicationDirPath + /gui/Import" did not work

    How do you define applicationDirPath? Does QML print any warnings?

  • Hi @sush123 ,i guess you need to set "folder" path of fileDialog

    here is a sample code, in which i set 3 different folder paths please have look into.If iam wrong with my understanding please do correct me.

      //Use this variable to set the path of the folder, change it according to your path
        property string folderPath: "C:/DummyFolder"
        property int index: -1
        FileDialog {
            id: fileDialog
            title: "Please choose a file"
            onAccepted: {
                console.log("You chose: " + fileDialog.fileUrls)
            onRejected: {
        Button {
            id: changeButton
            text: "Open"
            onClicked: {
             //You can set the path of the folderPath here also
             // folderPath = "C:/DummyFolder1/Images" etc etc
                if(parent.index > 2)
                    parent.index = -1;
                switch(parent.index) {
                case 0:
                    fileDialog.folder = fileDialog.shortcuts.documents
                case 1:
                    fileDialog.folder = fileDialog.shortcuts.desktop
                case 2:
                    fileDialog.folder = "file:///" + parent.folderPath

  • works fine with below solution :

    folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation) +"/gui/Import"

Log in to reply