Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to use dynamic path to read file on FileDialog open
Forum Update on Monday, May 27th 2025

How to use dynamic path to read file on FileDialog open

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 624 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sush123
    wrote on 12 Apr 2020, 16:22 last edited by sush123 4 Dec 2020, 16:26
    #1

    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 :)

    1 Reply Last reply
    0
    • S Online
      S Online
      sierdzio
      Moderators
      wrote on 14 Apr 2020, 06:27 last edited by
      #2

      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?

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shrinidhi Upadhyaya
        wrote on 14 Apr 2020, 09:07 last edited by Shrinidhi Upadhyaya
        #3

        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()
                }
            }
        

        Shrinidhi Upadhyaya.
        Upvote the answer(s) that helped you to solve the issue.

        1 Reply Last reply
        1
        • S Offline
          S Offline
          sush123
          wrote on 16 Apr 2020, 08:34 last edited by
          #4

          works fine with below solution :

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

          1 Reply Last reply
          0

          1/4

          12 Apr 2020, 16:22

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved