How to open file and Directory in QT
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:
- subclassing QfileDialog class or
- using the
QFileSystemModel
with aQTreeView
.
精彩评论