[Qt-interest] QFileDialog::getOpenFileName Directory name

K. Frank kfrank29.c at gmail.com
Tue Jun 8 18:39:01 CEST 2010


Hi Yogesh -

On Tue, Jun 8, 2010 at 11:24 AM, yogesh upreti <yogesh.upreti at gmail.com> wrote:
> Hi Frank,
> Thanks for an elobrate reply. Sometimes it becomes difficult to explain the
> exact question :). This is exactly what I was looking for.
> Do you think it makes a differance when I use
> "F:/Data/" and "F:\\Data\\" (I am using the first one, may be that's the
> diffarance maker)

I really don't know whether the choice of path separator matters.
I wouldn't think it would, but I didn't try it.

Your result does seem odd -- why should it matter whether you
use a string literal or first package it into a QString?

By the way, i should have mentioned:  I did my test using qt 4.6.1
built with 32-bit mingw g++ 4.41 on 64-bit windows 7.

> Let me check it, with your code lines.

Please let me know what you see on your end.

Best.

K. Frank

> Qt is so good but I hate when I get struck with something which is not even
> 1% relevant for my code.
>
> Thanks again
> - Yogesh
>
> On Tue, Jun 8, 2010 at 5:17 PM, K. Frank <kfrank29.c at gmail.com> wrote:
>>
>> Hi Yogesh -
>>
>> On Tue, Jun 8, 2010 at 7:35 AM, yogesh upreti <yogesh.upreti at gmail.com>
>> wrote:
>> > Hallo Oliver,
>> > I was aware about the fact that its a static method, so I first wrote
>> > the
>> > code without an instance.
>>
>> (I think the issue of whether you invoke the static method with
>> Class::staticMethod() of instanceOfClass.staticMethod() is a
>> red herring.)
>>
>> > But my problem is this. If I write:
>> >  QString filePath = QFileDialog::getOpenFileName(this, "Caption",
>> > "/foo/bar/baz/", "*.D"); (this is what I mean giving dir path
>> > explicitly);
>> > it opens the dialog box in "/foo/bar/baz/" dir.
>> > But if I write it
>> > QString directoryPath = "/foo/bar/baz/";
>> > QString filePath = QFileDialog::getOpenFileName(this, "Caption",
>> > directoryPath, "*.D");
>>
>> I believe I understand what you are asking:  Why is the behavior
>> different when the argument is passed as a string literal rather
>> then when passed as an instance of a QString?
>>
>> > it dosen't open it in "/foo/bar/baz/" instead it opens it in current
>> > working
>> > dir. I am using Nokia Qt SDK 2010 for compiling my programme.
>>
>> I am not able to reproduce the behavior you see.
>>
>> In some test code I open eight QFileDialogs, four times with
>> string literals, and four times with matching QStrings:
>>
>>  QString filePathA1 = QFileDialog::getOpenFileName(0, "Caption",
>> "c:\\", "*.log");
>>  QString filePathA2 = QFileDialog::getOpenFileName(0, "Caption",
>> "c:\\junk", "*.log");
>>  QString filePathA3 = QFileDialog::getOpenFileName(0, "Caption",
>> "c:\\junk\\", "*.log");
>>  QString filePathA4 = QFileDialog::getOpenFileName(0, "Caption",
>> "c:\\junk\\junk", "*.log");
>>
>>  QString dirp1 = "c:\\";
>>  QString dirp2 = "c:\\junk";
>>  QString dirp3 = "c:\\junk\\";
>>  QString dirp4 = "c:\\junk\\junk";
>>
>>  QString filePathB1 = QFileDialog::getOpenFileName(0, "Caption",
>> dirp1, "*.log");
>>  QString filePathB2 = QFileDialog::getOpenFileName(0, "Caption",
>> dirp2, "*.log");
>>  QString filePathB3 = QFileDialog::getOpenFileName(0, "Caption",
>> dirp3, "*.log");
>>  QString filePathB4 = QFileDialog::getOpenFileName(0, "Caption",
>> dirp4, "*.log");
>>
>> The path c:\ exists on my system; the path c:\junk\ does not.
>>
>> I get exactly the same behavior with the string literal and with the
>> QString.
>>
>> Specifically:
>>
>> case 1:  dialog opens in c:\ with no text loaded in the "File name:" combo
>> box
>> case 2:  dialog opens in c:\ with "junk" loaded in the "File name:" combo
>> box
>> case 3:  dialog opens in the current working directory with no text in
>> the combo box
>> case 4:  dialog opens in the current working directory with "junk" in
>> the combo box
>>
>> As I interpret it, in cases 3 and 4 the QFileDialog looks for the
>> directory
>> c:\junk\, and, because it doesn't exist, defaults to the current working
>> directory.
>>
>> Again, to emphasize, I get the same behavior both using a string literal
>> and a QString in the call to QFileDialog::getOpenFileName(...).
>>
>> The only thing I can think of (This is pure speculation on my part; I
>> have no idea what is going on here.) is that maybe QString is doing
>> some sort of translation or character-set encoding that is causing
>> your directoryPath variable not to match the actual name of your
>> directory, causing getOpenFileName(...) to default to the current
>> working directory.  But I have know idea why it would do that.
>>
>> Good luck.
>>
>> K. Frank
>>
>>
>> > I will try in the way you said (example with fd.exec()) and give the
>> > feedback.
>> >
>> > Thanks
>> > Yogesh
>> >
>> > ...




More information about the Qt-interest-old mailing list