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:
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.
Use proxy model for file dialog
My code is the following:
#include <QSortFilterProxyModel>
#include <QFileSystemModel>
// Custom proxy for filtering executables
class FileFilterProxyModel : public QSortFilterProxyModel
{
private:
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
};
bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
QFileInfo file( fileModel->filePath(sourceModel()->index(sourceRow, 0, sourceParent)) );
if (fileModel!=NULL && file.isExecutable())
return true;
else
return false;
}
// usage of proxy model
QFileDialog dialog( this, tr("Choose a file"));
FileFilterProxyModel* proxyModel = new FileFilterProxyModel;
dialog.setProxyModel(proxyModel);
dialog.setOption(QFileDialog::DontUseNativeDialog); // required by proxy model
if( dialog.exec() == QDialog::Accepted ) {
...
}
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)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With