使用QFileDialog仅显示Ubuntu上的目录和可执行文件 [英] show only directories and executables on Ubuntu using QFileDialog

查看:277
本文介绍了使用QFileDialog仅显示Ubuntu上的目录和可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Ubuntu上创建一个QFileDialog,它将允许用户选择一个可执行文件,目的是该文件是桌面应用程序(即类似于 .exe Windows上的可执行文件的子集.

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

在Windows上,这是通过setNameFilter查找"(*.exe)"文件来实现的,但是由于Ubuntu显然不对可执行文件使用扩展名,因此您需要使用QDir::Filters方法.

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();

但是它实际上具有过滤掉99%的文件系统条目(包括几乎每个目录)的作用,使得无法导航.

but it actually has the effect of filtering out 99% of file system entries, including almost every directory, making it impossible to navigate.

似乎QFileDialog::setFilter函数将所有过滤器和权限应用于它所查看的每个文件,问题是目录和可执行程序是(几乎)互斥的,我无法在Ubuntu上弄清楚实现任何目录,或仅可以作为程序执行的文件"的正确组合是什么.

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".

我还尝试了AllDirsDirsExecutableAllEntries等的大多数排列,所以我不认为它像缺少一个属性一样简单.

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

结果:

  1. 所有内容都被过滤掉
  2. 所有内容都被过滤掉
  3. 没有任何东西被过滤掉

关于PyQt的相关问题,但从未得到解答,而且我不确定该问题的OP是否希望目录可见.

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

我的代码如下:

#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 ) {
    ...
}

这显示了在Linux和Windows(Qt 4.8.6)上都经过测试的可执行文件和文件夹

This shows executables and folders, tested on both Linux and Windows (Qt 4.8.6)

完整资源

另请参见 QFileDialog:是否可以仅过滤可执行文件(在Linux下)?

这篇关于使用QFileDialog仅显示Ubuntu上的目录和可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆