tags:

views:

285

answers:

2

Hi, I want to open Directory and file using the same function. Is it possible to do the same in QT. I used

QString directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
      "",
     QFileDialog::ShowDirsOnly
     | QFileDialog::DontResolveSymlinks);

Here i can open only directory. How to open both file and directory using single function

+1  A: 

Well, I don't think QFileDialog can do this job... Maybe you can use a QDirModel. That should do the trick... On clicking your "Browse"-button or something like that, you open a widget with a QTreeView using a QDirModel, there you can take the selected item and its path as your file/directory. For further information, see the documentation and the Dir View example in Qt Assistant.

EDIT: It is recommended to use QFileSystemModel instead of QDirModel. Thanks to Patrice for the advice.

Exa
QDirModel must not be used anymore as specified in the documentation: http://doc.trolltech.com/4.6/qdirmodel.html#details
Patrice Bernassola
+1  A: 

You must use the getOpenFileName function to get files and getExistingDirectory function to get directories. You can not use a unique function from QFileDialog class to do both in the same time.

You have to do it by yourself by:

Patrice Bernassola