Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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
                {
                    openFileDialog.open();
                }
            }
    

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

        onAccepted: {
            selectedFilePath  = folder
            model.readPointsFromFile(selectedFilePath)      
        }
    

    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)
                fileDialog.close()
            }
            onRejected: {
                console.log("Canceled")
                fileDialog.close()
            }
        }
    
    
        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;
    
                parent.index++;
    
                switch(parent.index) {
                case 0:
                    fileDialog.folder = fileDialog.shortcuts.documents
                    break;
                case 1:
                    fileDialog.folder = fileDialog.shortcuts.desktop
                    break;
                case 2:
                    fileDialog.folder = "file:///" + parent.folderPath
                    break;
                }
    
                fileDialog.open()
            }
        }


  • works fine with below solution :

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


Log in to reply