PDA

View Full Version : Problem with QFileDialog::getOpenFileName()



spud
31st October 2007, 15:56
Hi,
in my app I let users select an image file with the following call:


QString fileName = QFileDialog::getOpenFileName(this);

The problem is on Windows when I select the folder "Sample Images" in "My Pictures", instead of opening the folder, the function returns "xxx.lnk".
I know the folder is just a shortcut, but since I haven't specified "DontResolveSymlinks" I don't know why this happens.
I tried adding "DontUseNativeDialog" as a workaround, and the Qt dialog managed to open the folder, but when I select a file it complains "File not found".

I'm not sure what I'm doing wrong. Qt Designer is able to follow those shortcut just fine.

jpn
31st October 2007, 16:06
Oh, does QFileDialog::getOpenFileName() let you choose a directory? Or do you mean QFileDialog::getExistingDirectory()?

spud
31st October 2007, 18:51
I don't want to choose a directory, but I have to double click them to navigate within the dialog.

jpn
1st November 2007, 18:41
Qt Designer is able to follow those shortcut just fine.
Aha, now I realize the problem:

with filter "Designer UI files (*.ui)" one can follow "Sample Images" shortcut because it's not acceptable file type
with filter "All Files (*)" one can't follow "Sample Images" shortcut because it's acceptable file type

So, basically the problem is with Windows file dialog since it treats a directory shortcut as a file... right? You could work this around with QFileInfo::isSymLink() and QFileInfo::sysLinkTarget(). However, I'd suggest constructing a filter from QImageReader::supportedImageFormats(). First of all, it solves the problem and secondly, the user will be able to choose only supported image types. ;)

spud
1st November 2007, 22:31
You're exactly right. Thanks for clearing up exactly why this happens! I found out that this is a bug (http://trolltech.com/developer/task-tracker/index_html?method=entry&id=181912) which is set to Fixed in 4.3.3. so for now I'll just ignore it.

I do pass on QImageReader::supportedImageFormats(), but it's good custom to provide the option "All files(*.*)". Even designer does it.;)