I'm using Qt QFileDialog::getSaveFileName and QFileDialog::getOpenFileNames for the user to select where to save files and what files to open in my app.
The third parameter of this function is the path where you want the window to open by default. In linux, How can I get the dialog to open in the user home, and in windows how can I get the dialog to open in the user user folder in win 7 or in 'My Documents' in win xp?
Currently I'm using the dialog like this: QFileDialog::getOpenFileNames(this, "Select a file to open...", HOME);
where HOME is a preprocessor macro that in UNIX is ~ and in windows is C:\
The Unix one does not work and opens the dialog in the same folder where the binary is.
Use QDir::homePath
.
QFileDialog::getOpenFileNames(this, "Select a file to open...", QDir::homePath())
Also if you want to apply a filter on existing files, you can try this:
QString filter = "File Description (*.extention)";
// For example: "Mpeg Layer 3 music files (*.mp3)"
QFileDialog::getOpenFileName(this, "Select a file...", QDir::homePath(), filter);
And then once user selected a file, absolute address of that file is returned by QFileDialog::getOpenFileName
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With