QFileDialog - how to specify home directory

rogergl picture rogergl · Dec 25, 2012 · Viewed 7.4k times · Source

I tried the following to open a file dialog that should display the users home directory:

QString fileName = QFileDialog::getOpenFileName(this,
                   tr("Select database"), QDir::homePath(), 
                   tr("Database Files (*.db *.sqlite)"));

The problem ist that QFileDialog always starts with the directory from where the application was started. Any ideas what I'm doing wrong ?

BTW: I'm using Qt 5 on Mac OS X.

Answer

Daniel Castro picture Daniel Castro · Dec 25, 2012

In Mac OSX, QDir::homePath() returns the content of the HOME environment variable. So if this variable is empty, it returns an empty string.

Ensure the 'Clear system environment' option (under Build Environment from the project settings in Qt Creator) is not checked. When you check this box, Qt will basically overwrite the value of every environment variable for your program, so it would appear to be empty.