收录日期:2021/01/22 02:45:07 时间:2010-03-31 05:08:01 标签:qt,qt4,qt-creator

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

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.

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: