5

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.

demonplus
  • 5,613
  • 12
  • 49
  • 68
rogergl
  • 3,501
  • 2
  • 30
  • 49

2 Answers2

8

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.

Daniel Castro
  • 1,290
  • 2
  • 11
  • 22
3

Use QDir::homePath() as suggested earlier or consider usage of Qt5's QStandardPaths class.

Oleg Shparber
  • 2,732
  • 1
  • 18
  • 19
  • The OP is already using `homePath()`. And as for `QStandardPaths`, looks like it still just calls `QDir::homePath()` when querying the home dir: http://qt.gitorious.org/qt/qtbase/blobs/stable/src/corelib/io/qstandardpaths_mac.cpp#line161 – jdi Dec 25 '12 at 21:19
  • I'm using QDir:homePath, but QFileDialog seems to ignore it. Even a hardcoded value is ignored. – rogergl Dec 25 '12 at 21:36
  • Sounds like a bug, because it works as expected on Linux. @rogergl Did you see [this](http://qt-project.org/forums/viewthread/9466)? It says about the same solution Daniel Castro did. – Oleg Shparber Dec 25 '12 at 21:39