[Qt-interest] QFileDialog::getOpenFileName Directory name [SOLVED]
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Wed Jun 9 16:33:58 CEST 2010
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
More information about the Qt-interest-old
mailing list