PDA

View Full Version : QFileDialog does not always return path - (bug?)



thefriedc
30th September 2010, 17:30
hi,

i just encountered a problem with QFileDialog. I use the static method "getExistingDirectory" to select a path, used in my software.
If i open the filedialog and click "Choose" without really choosing a dir, the return value is an empty string.
I would have been expected at least the path to the directory which has been showed, when opening the filedialog.
What if a user is happy with the path suggested at opening the dialog and just clicks "Choose" - i just get an empty string and do not know what has been selected.
I do not set a default path at opening the dialog yet. but doing so would not solve the problem.

if i use the dialog to browse to a specific dir and select it - i get the path as return value. if i open the dialog again - the previously selected dir is shown. if i click "Choose" now, the return value is an empty string again.

is this a bug? i do understand why "Cancel" returns an empty string, but "Choose" too?

to clarify my problem:

i am not able to investigate, if the user clicked "cancel" or if the user is satisfied with the path suggested at showing the filedialog and just clicked "choose"
in both cases i just get an empty string.

thnx, chris

tbscope
30th September 2010, 19:13
I'm not sure that bevaviour is a bug.

Quoting the documentation:

The dialog's working directory is set to dir, and the caption is set to caption. Either of these may be an empty string in which case the current directory and a default caption will be used respectively.

So, you always set a directory, even if you leave it empty.
If it is empty, it defaults to the current directory.

If the return is empty, nothing changed

thefriedc
30th September 2010, 21:28
If the return is empty, nothing changed

yes, this is my problem - because there are two possible reasons for an empty return:

1) the user clicked "cancel"
2) the user is satisfied with the initially selected directory and clicked "choose"

in the latter case i have to proceed with my code and "install" a few things in the selected directory.
so if i do not get a return i simply cannot decide if the user clicked "cancel" or "choose"

i hope i did explain the problem somehow understandable ...

Lykurg
30th September 2010, 21:46
I understand what you mean, but I can't reproduce your problem here. Please could you show us the code your are exactly using. Maybe it is a problem with PyQt or whatever you use.

squidge
30th September 2010, 23:09
so if i do not get a return i simply cannot decide if the user clicked "cancel" or "choose"QFileDialog is model, it returns whether the cancel or choose button is pressed...

ChrisW67
1st October 2010, 03:45
On my Linux box if I pass an empty string as the dir argument then immediately press Choose it get the current working directory returned. If I pass a starting directory and immediately press Choose then I get that directory back. I only get an empty string return if the user presses Cancel.

Which platform is displaying this problem? On Windows and Mac the native dialog is used and this might account for the difference.

thefriedc
1st October 2010, 09:21
ah, great - this is good to know.

i only tested it under osx. maybe there is really a difference because of using the native filedialog.

thank you very much for this information

thefriedc
1st October 2010, 09:30
QFileDialog is model, it returns whether the cancel or choose button is pressed...

yes - i could create an instance of QFileDialog and then connect to the rejected() and accepted() signals (if this is what you mean)

at the moment i just use a static method for displaying the filedialog which is really handy and just a one-liner. maybe i should really switch to the instance approach.

thanks

squidge
1st October 2010, 12:29
No, I mean more like:



QFileDialog dialog(this);
if (dialog.exec())
(... user has chosen OK ...)

ChrisW67
1st October 2010, 23:09
Try the QFileDialog::DontUseNativeDialog option to test the theory.

thefriedc
3rd October 2010, 11:05
Yes, it works, when setting "DontUseNativeDialog". I always get the current selected path when clicking "Choose".

thanks a lot!

thefriedc
3rd October 2010, 11:22
omg - i just investigated something "terrible"

the native filechooser dialog returns the current path as well. there is just one difference, when directly choosing the suggested path - there is a trailing "/" at the path string.
in my code i split the return value by "/" and take the last item. therefore i get an empty string as last item. i only checked this splitted value.

im really sorry for this false alarm.

chris