I'm trying to create a QFileDialog
on Ubuntu that will allow the user to select an executable file, with the intent being that the file is a desktop application (i.e. analogous to the .exe subset of executable files on Windows).
On Windows, this is achieved using setNameFilter
to look for "(*.exe)"
files, but since Ubuntu obviously doesn't use extensions for executables, you need to use the QDir::Filters
method.
You'd think that the following would do the trick
QFileDialog dialog;
dialog.setFilter(QDir::AllDirs | QDir::Executable);
// ...
dialog.exec();
but it actually has the effect of filtering out 99% of file system entries, including almost every directory, making it impossible to navigate.
It seems like the QFileDialog::setFilter
function applies all the filters and permissions to every file and directory it looks at, with the problem being that directories and executable programs are (pretty much) mutually exclusive, and I can't figure out on Ubuntu what the right combination is to achieve "Any directory, or only those files which can be executed as a program".
I've additionally tried most permutations of AllDirs
, Dirs
, Executable
, AllEntries
, etc. so I don't think it's as simple as one missing property.
Some other permutations I've tried:
dialog.setFilter(QDir::AllDirs | QDir::Executable | QDir::Files); // 1
dialog.setFilter(QDir::AllDirs | QDir::Executable | QDir::Files |
QDir::Readable); // 2
dialog.setFilter(QDir::AllDirs | QDir::Executable | QDir::Files |
QDir::Readable | QDir::Writeable); // 3
With the results:
- everything is filtered out
- everything is filtered out
- nothing is filtered out
There's a related question regarding PyQt, which was never answered, and also I'm not sure the OP of that question wanted directories to be visible.
I use QDir. Works with linux fine, here's an example:
If it doesn't get you what you want just remove the QDir::Executeable
I have written a backup program with QDir based search algorithm (recursion) and never had a problem in linux or windows. Maybe it does work for you.
Just put in the path that you wanna look through, and include , and ;)
Have a nice day
Use proxy model for file dialog
My code is the following:
This shows executables and folders, tested on both Linux and Windows (Qt 4.8.6)
Full sources
See also QFileDialog: is it possible to filter only executables (under Linux)?