简体   繁体   English

在 QFileDialog 中过滤

[英]Filtering in QFileDialog

I would like to filter the files that are shown in a QFileDialog more specifically than just by file extensions.我想更具体地过滤QFileDialog中显示的文件,而不仅仅是通过文件扩展名。 The examples I found in the Qt documentation only show filters like Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml) and such.我在 Qt 文档中找到的示例仅显示过滤器,如Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)等。 In addition to this I would also like to specify a filter for files that should not show up in the file dialog, eg XML files (*.xml) but not Backup XML files (*.backup.xml) .除了这个我也想指定文件过滤器,应该在文件对话框中显示出来,如XML files (*.xml)而不是Backup XML files (*.backup.xml)

So the problem I have is that I would like to show some files in the file dialog that have certain file extension, but I would not like to show other files with a specific file name suffix (and the same file extension).所以我的问题是我想在文件对话框中显示一些具有特定文件扩展名的文件,但我不想显示具有特定文件名后缀(和相同文件扩展名)的其他文件。

For example:例如:

Files to show:要显示的文件:

file1.xml  
file2.xml

Files not to show:不显示的文件:

file1.backup.xml  
file2.backup.xml

I would like to ask if it is possible to define filters like these for a QFileDialog ?我想问一下是否可以为QFileDialog定义这样的过滤器?

I believe what you can do is:我相信你能做的是:

  1. Create a custom proxy model.创建自定义代理模型。 You can use QSortFilterProxyModel as a base class for your model;您可以使用QSortFilterProxyModel作为模型的基类;
  2. In the proxy model override the filterAcceptsRow method and return false for files which have the ".backup."在代理模型中,覆盖filterAcceptsRow方法并为具有“.backup”的文件返回 false word in their names;他们名字里的字;
  3. Set new proxy model to the file dialog: QFileDialog::setProxyModel ;在文件对话框中设置新的代理模型: QFileDialog::setProxyModel ;

Below is an example:下面是一个例子:

Proxy model:代理模式:

class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
    virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
};

bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
    QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
    return fileModel->fileName(index0).indexOf(".backup.") < 0;
    // uncomment to call the default implementation
    //return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}

dialog was created this way:对话框是这样创建的:

QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
dialog.setProxyModel(new FileFilterProxyModel);
dialog.setNameFilter("XML (*.xml)");
dialog.exec();

The proxy model is supported by non-native file dialogs only.仅非本机文件对话框支持代理模型。

The solution of @serge_gubenko is working well. @serge_gubenko 的解决方案运行良好。 Create your own ProxyModel by inheriting from the QSortFilterProxyModel .通过继承QSortFilterProxyModel创建您自己的ProxyModel

class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
    virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
};

bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
    // Your custom acceptance condition
    return true;
}

Just make sure to set DontUseNativeDialog before setting the Proxy model ( Edit : @serge_gubenkos answer does that now).只需确保设置代理模型之前设置DontUseNativeDialog编辑:@serge_gubenkos 答案现在这样做)。 Native dialogs do not support custom ProxyModel s.本机对话框不支持自定义ProxyModel

QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
dialog.setProxyModel(new FileFilterProxyModel);
dialog.setNameFilter("XML (*.xml)");
dialog.exec();

It took quite some time for me to find this out.我花了很长时间才发现这一点。 This was written here这是写在这里

Okay, I've used it with QFileDialog object.好的,我已经将它与QFileDialog对象一起使用了。 And this only shows me the files listed in the appropriate directory.这只会向我显示相应目录中列出的文件。 It is excellent to just choose the files to be processed.只选择要处理的文件非常好。 For example, an XML file, a PNG image, etcetera.例如,XML 文件、PNG 图像等。

Here I present my example我在这里展示我的例子

 OlFileDialog QFileDialog (this); 
 QString slFileName; 
 olFileDialog.setNameFilter (tr ("Files (* xml)")); 
 olFileDialog.setFileMode (QFileDialog :: anyfile); 
 olFileDialog.setViewMode (QFileDialog :: Detail); 
 if (olFileDialog.exec ()) 
     olFileDialog.selectFile (slFileName); 
 else 
     return; 

The dialog box will display only presents xml files.该对话框将只显示呈现的 xml 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM