Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the Qt how to open QFileDialog::getOpenFileNames in user home

Tags:

c++

qt

qt4

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.

like image 206
Topo Avatar asked Jun 13 '12 06:06

Topo


Video Answer


2 Answers

Use QDir::homePath.

QFileDialog::getOpenFileNames(this, "Select a file to open...", QDir::homePath())
like image 189
Oleh Prypin Avatar answered Oct 30 '22 11:10

Oleh Prypin


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.

like image 43
frogatto Avatar answered Oct 30 '22 12:10

frogatto