FileDialog : How do I configure to open a specified path...?



  • Hello ;

    my QT QUICK context question :

    How do I configure the FileDialog to open the windows default pictures directory or a specified folder path ...?

        FileDialog {
            id: picFileDialog
            nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
            title: "Please choose a file"
    
            folder:"/"  <----- Code I Need
    
            onAccepted: {
                id: picFileDialog
                console.log(" ...")
            }
            onRejected: {
                console.log("Canceled")
            }
        }
    

    Thanks.
    MSD.



  • There's 2 ways that I can think of. The first and easiest way is to use the provided shortcuts.

    FileDialog {
        //... 
        folder: shortcuts.pictures
    } 
    

    The second way would be to expose a variable containing the path from QStandardPaths in C++. This might used if you need a standard path that's not available from the shortcuts.

    QUrl picturesPath{ QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).at(0) };
    view.rootContext()->setContextProperty("picturesPath", &picturesPath);
    

    I could also imagine making a struct of some kind that acts similar to shortcuts in FileDialog, which contains members for whichever standard paths you may need in your application.

    Sorry if the code is wrong (I'm typing from my phone), but this should get the idea across.



  • @JordanHarris
    Thank you JordanHarris, I put this codes and it works fine :

     FileDialog {
    ...   
         folder: "file:///C:/Users/Admin/Pictures/"
    ...
    


  • That'll work on that particular machine, for that user, and may be okay for testing, but you really shouldn't use a specific path like that. Did you try the shortcuts option I mentioned? The Qt Quick music player example does something similar to the second option I showed. I suspect that example might have been created before shortcuts was made available though. This is the exact code from that example:

    QQmlApplicationEngine engine;
    const QStringList musicPaths = QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
    const QUrl musicUrl = QUrl::fromLocalFile(musicPaths.isEmpty() ? QDir::homePath() : musicPaths.first());
    engine.rootContext()->setContextProperty(QStringLiteral("musicUrl"), musicUrl);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.