Important: Please read the Qt Code of Conduct -

FileDialog setting initial directory

  • I have a FileDialog defined like so:

    FileDialog {
                id: saveDialog
                property Action fileSaveAction: Action{}
                onAccepted: {
                    console.log("You chose: " + saveDialog.fileUrl)
                onRejected: {

    I invoke it like this:

                        saveDialog.folder  =  "file://" + dir
                        saveDialog.nameFilters = [ "Program files (*.xm)" ]
                        saveDialog.title = "Save Weld Program As ,,,"
                        saveDialog.fileSaveAction = actionSavePrgm

    However, my "dir" is given to me as a native path. This method of setting folder fails with:

    shellItem: SHCreateItemFromParsingName(file:///c)) failed (The operation completed successfully.)

    I tried this:

    saveDialog.folder  =  dir

    I don't get an error, but it does not set the default directory correctly. It just goes to the local directory of where the program is running from. The directory should be going to:

    C:\ProgramData\XM Editor\RnD\programs

    The description of the property "folder" is as follows:

    What kind of url/path does folder need?

  • I managed to use QDir to convert my path into one that uses "/":

    C:/ProgramData/XM Editor/RnD/programs

    Now when trying to set that folder property I get this error:

    shellItem: SHCreateItemFromParsingName(file:///c/ProgramData/XM Editor/RnD/programs)) failed (An attempt was made to reference a token that does not exist.)

  • Now I am really confused:

    saveDialog.folder  =  "file:///" + dir

    Apparently it needs 3 slashes.

  • Lifetime Qt Champion


    IIRC: file:// is the protocol and the third slash is the start of your path.

  • Does this mean I have to special case the Windows path? In Linux our paths generally come with "/" at the beginning because we use absolute paths. Is there a way to get a Windows path that has this "/" already? I tried using QDir to spit out a path, but it does not add the "/" at the beginning.

  • Hmmm, the more I dig into FileDialog the more I am really confused. Apparently it cannot be used as a "SaveAs" dialog. Then I looked for a Save/SaveAs dialog solution and find there is not such a thing in QtQuick/QML.

    I then found this article. Please tell me I don't need to use private headers to create a QtQuick dialog to do Save/SaveAs functionality natively in QtQuick app. Please?

  • Lifetime Qt Champion

    Ok, as you wish, I won't tell it. You can hack Qt's sources if you prefer.

  • Ouch! Thanks for the confirmation. I know sometimes its hard to be the messenger. Thanks again.

  • Lifetime Qt Champion

    By the way, where do you get dir from ?

  • We have an existing class with methods that spit out the directory. It provides this dir for a lot of different places in the code. I found that the "folder" property would accept the native formatting as long as there are 3 "/" before it. So the formatting of the path was less important than the "/"s.

Log in to reply