QFileDialog:是否可以仅过滤可执行文件(在Linux下)? [英] QFileDialog: is it possible to filter only executables (under Linux)?

查看:241
本文介绍了QFileDialog:是否可以仅过滤可执行文件(在Linux下)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用QFileDialog来让用户选择一个可执行文件.该对话框应仅显示目录以外的实际可执行文件.

I want to use QFileDialog to have the user select an executable. The dialog should show only actual executables, aside from directories.

我的Windows版本工作正常,只需检查扩展名是否为exe.但是,在Linux中,我无法按照自己的意愿进行操作.

My Windows version works just fine, simply checking if the extension is exe. However, in Linux, I don't manage to do it as I want.

在C ++中,我的尝试看起来像这样:

In C++, my attempt looked like this:

QString target_dir = ...
QFileDialog file_dialog;
file_dialog.setFilter(QDir::Executable | QDir::Files);
QString file = file_dialog.getOpenFileName(this, tr("Open Exectuable"), target_dir);

但是,此代码导致显示所有个文件.

However, this code results in displaying all files.

我尝试添加其他过滤器,但到目前为止没有任何效果.在StackOverflow上已经存在两个与我的问题基本相同的问题,但都没有实际答案:

I experimented with adding some other filters, but nothing worked so far. There are already two questions on StackOverflow that are essentially the same as mine, both without an actual answer:

在Linux上的QFileDialog中过滤可执行文件

仅显示目录并使用QFileDialog在Ubuntu上运行可执行文件

有人知道该怎么做吗?还是QFileDialog根本无法做到这一点?可以做到吗?或者识别可执行文件通常不是那么简单?

Does anybody know how it can be done? Or is QFileDialog simply not able to do it? Can it be done at all or is recognizing executables not that simple in general?

(注意:我使用Qt 4.8.5,因为我使用了与Qt 5不兼容的第三方代码).

(Note: I work with Qt 4.8.5 since I use third party code that is incompatible with Qt 5, if that matters.)

(注意:由于它也与Python相关,因此未将其标记为C ++.)

(Note: Haven't tagged this as C++ since it's also relevant for Python.)

推荐答案

如果使用本机文件对话框,则某些设置无效.

if you use native file dialogs some settings have no effect.

这应该有效:

   QFileDialog dlg(this, tr("Select executable"));
   dlg.setOption(QFileDialog::DontUseNativeDialog, true);
   dlg.setFilter(QDir::Executable | QDir::Files);

请注意,这只会归档可执行文件.要同时显示文件夹,没有已知的解决方案.

Note that this will filer only executables. TO show folders at same time there is no known solution.

这篇关于QFileDialog:是否可以仅过滤可执行文件(在Linux下)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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