Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show only directories and executables on Ubuntu using QFileDialog

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:

  1. everything is filtered out
  2. everything is filtered out
  3. 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.

like image 695
Nicolas Holthaus Avatar asked Nov 21 '22 08:11

Nicolas Holthaus


1 Answers

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)?

like image 75
Yaroslav Kornachevskyi Avatar answered Nov 22 '22 22:11

Yaroslav Kornachevskyi