[Qt-interest] QFileDialog::getOpenFileName Directory name [SOLVED]
yogesh upreti
yogesh.upreti at gmail.com
Wed Jun 9 17:18:51 CEST 2010
Hi All,
It took me 3 days to solve something which wasn't even an error, and I am so
happy that even if someone says, "I am a bullshit programmer", I will hug
him/her. Anyway I am still not sure why this error occurred, because the
truth is I removed everything and wrote the code again instead of debugging
(not a proper way but I have the results in the end)
Here are the highlights.
1. I used and instance of QFileDialog. I know its a gray area to work with
but the example in documentation of Qt shows it in this way.
2. I used "\\" for Directory name in string variable.
3. QT ROCKS !!!!
Thanks all,
I will be present here with another question because no happiness stays for
long. :)
Regds
Yogesh
Date: Wed, 9 Jun 2010 16:33:58 +0200
From: <Oliver.Knoll at comit.ch>
Subject: Re: [Qt-interest] QFileDialog::getOpenFileName Directory name
[SOLVED]
To: <qt-interest at trolltech.com>
Message-ID:
<C10F29AB06447B4881FC0DE1E302E2F206A5DE2DAC at sg000036.corproot.net>
Content-Type: text/plain; charset="us-ascii"
K. Frank wrote on Wednesday, June 09, 2010 4:02 PM:
> ...
> I do think that his using an instance of QFileDialog is a red herring.
> I agree that it is better style to call the static member function
> with
>
> QFileDialog::getOpenFileName(...);
>
> rather that with
>
> instanceOfAQFileDialog.getOpenFileName(...);
>
> but my understanding is that these two calls are actually the same.
They are the same in that both time the *static* method is called, yes. But
the second example is totally bad style, because it implies to the reader
that a non-static method is called. And I think this is also the
mis-conception that Yogesh is having: calling setDirectory() on the
*instance* has absolutely *no effect* on the successive *static* method
call! Even if it is (badly) written as:
instanceOfAQFileDialog.getOpenFileName(...);
And in fact, this is exactly the reason why you should write
QFileDialog::getOpenFileName(...);
such that you avoid these kind of mis-conceptions (and e.g. in
Eclipse/Java/StyleCheck you would get a big fat warning when writing such
code)! So again, THIS WON"T WORK AS EXPECTED:
QFileDialog fd;
fd.setDirectory("/totally/useless/to/set/this/here/");
QString filePath = fd.getOpenFileName(...);
> But, unless there is a typo in what Yogesh posted, how could it be
> his code?
>
> It's very odd. I don't see how there could be a difference between
> passing a string litereal and a QString to
> QFileDialog::getOpenFileName. (And I didn't see any difference in my
> test.)
I am not sure what you refer now to "his code". Off course there is no
difference between passing a string literal (with implicit conversion to
QString) and a QString, as I also posted in the above example; we are on the
same page here. But the fact that Yogesh apparently tries to setDirectory()
and then use the static method makes me think that he is not only on a
different page, but reading an entirely different book ;)
Anyway, as to conclude this topic, here is a fully working example for
Yogesh to study, showing both static vs non-static flavours:
#include <QtGui/QApplication>
#include <QtGui/QFileDialog>
#include "MainWindow.h"
// Assumes off course that c:\temp\ exists (on Windows - change to /tmp/ as
appropriate)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFileDialog fd;
QString initialDirectory = "c:/temp/";
QString filePath;
fd.setDirectory(initialDirectory);
if (fd.exec()) {
filePath = fd.selectedFiles().at(0);
qDebug("Selected filepath (instance): %s", qPrintable(filePath));
}
filePath = QFileDialog::getOpenFileName(0, "Open", initialDirectory);
if (!filePath.isNull()) {
qDebug("Selected filepath (static): %s", qPrintable(filePath));
}
return 0;
}
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100609/0bd6eea6/attachment.html
More information about the Qt-interest-old
mailing list