FileDialog : How do I configure to open a specified path...?
-
wrote on 26 Apr 2016, 06:23 last edited by MSDQuick
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. -
wrote on 26 Apr 2016, 07:28 last edited by JordanHarris
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.
-
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.
wrote on 28 Apr 2016, 02:55 last edited by@JordanHarris
Thank you JordanHarris, I put this codes and it works fine :FileDialog { ... folder: "file:///C:/Users/Admin/Pictures/" ...
-
wrote on 28 Apr 2016, 03:26 last edited by JordanHarris
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);
1/4