Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QFileDialog::getOpenFileName doesn't set the initial directory on Mac OS 10.8 Mountain Lion

Tags:

macos

qt

qt4

I can not change the current directory with QFileDialog with Qt 4.8. The same code works fine on Windows and Mac OS 10.6 Snow Leopard. It also works fine if I don't use the native Mac OS X dialog.

This works:

fn=QFileDialog::getOpenFileName(this,"Select File","/Users/myuser/Desktop",QString(),0,QFileDialog::DontUseNativeDialog);

This doesn't work:

fn=QFileDialog::getOpenFileName(this,"Select File","/Users/myuser/Desktop");

It looks like if most of the time it opens the last path of the last call to getOpenFileName.

like image 731
Thomas Rauscher Avatar asked Apr 24 '13 14:04

Thomas Rauscher


1 Answers

Got the same issue with Qt5.2.0 on Mavericks... I found a work around: append a dummy file name to the directory you want to select. However, be sure not to do this on Windows because the user will see it.

QString dir = "/Users/myuser/Desktop";
#if defined(__APPLE__)
dir += "/MyFile.txt";
#endif
fn = QFileDialog::getOpenFileName(this, "Select File", dir);

Also, for those like me that instantiate a file dialog because they need more options you can also do:

QFileDialog fileDialog(this, "Select File");
#if defined(__APPLE__)
fileDialog.selectFile(dir + "/MyFile.txt");
#else
fileDialog.setDirectory(dir);
#endif
...
like image 120
jng Avatar answered Oct 04 '22 01:10

jng