[Qt-interest] QFileDialog::getOpenFileName Directory name

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Tue Jun 8 10:39:14 CEST 2010


yogesh upreti wrote on Tuesday, June 08, 2010 8:41 AM:

> Hallo Alex,
> No there is no specific reason to make the instance of the class, but
> this code is also ignoring the directory if I use QString var and
> works if I specify dir explicitly. :( 

What do you mean with "it does not work when using QStrinv var, but it works when you specify the directory explicitly?" - how do you explicitly specify the directory?

I think there is still some confusion on your side with regards to the static getOpenFileName() method: there is NO NON-static method getOpenFileName() in the class QFileDialog!

So the following is actually WRONG (even though it actually compiles - in Java/Eclipse and the proper tools installed you get a warning though in such a case):

  QFileDialog fd;
  String filePath = fd.getOpenFileName(...); // WRONG!!! This method is STATIC!

Once you realise this and since I strongly assume you know the restrictions of static methods ("no access to member variables") you must also instantly realise that a call to

  fd.setDirectory(directoryPath);

can't possibly have an effect on the result of the call to the STATIC method getOpenFileName()! This is a very basic OO-concept (in any OO-language).

So as already described in another post use the static method correctly, e.g.

 QString directoryPath = "/foo/bar/baz/";
 QString filePath = QFileDialog::getOpenFileName(this, "Caption", directoryPath);

or use an INSTANCE of QFileDialog (can be useful if you want to set extra-options such as file filters):

  QString directoryPath = "/foo/bar/baz/";
  QFileDialog fd;
  fd.setDirectory(directoryPath);
  QStringList selectedFiles;
  if (fd.exec())
     selectedFiles = dialog.selectedFiles();

Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list