Is it possible to show a QFileDialog where the user can select a file or a directory, either one?
QFileDialog::getOpenFileName()
accepts only files, while QFileDialog::getExistingDirectory()
is directories-only, but I need to show a file dialog that can accept both a file or a directory (it makes sense for my program). QFileDialog::Options
didn't have anything promising.
QFileDialog currently does not support this. I think the main problem for you here is that the FileMode is not a Q_FLAGS and the values are not power of 2, either, and so, you cannot write this to solve this issue.
To solve this, you would need quite a bit of fiddling, e.g.:
Override the open button click operation.
Get the "treeview" indices properly for both files and directories.
My attempt below demonstrates the former, but I did not really go as far as solving the second because that seems to involve some more fiddling with the selection model.
main.cpp
main.pro
Build and Run
What worked for me was to use:
as suggested here, or
as suggested here, or
as suggested here. Credits for the original authors and me.