在QFileDialog中过滤 [英] Filtering in QFileDialog
问题描述
我想过滤 QFileDialog
中显示的文件,而不仅仅是文件扩展名。我在Qt文档中找到的例子只显示像 Images(* .png * .xpm * .jpg);;文本文件(* .txt);; XML文件(* .xml) code>等等。除此之外,我还想指定一个文件对话框,例如 not ,
XML文件(* .xml)
但不是备份XML文件(* .backup.xml)
。
所以我遇到的问题是,我想在文件对话框中显示一些具有特定文件扩展名的文件,但是我不想显示具有特定文件名后缀的其他文件(和相同的文件扩展名)。
例如:
要显示的文件:
file1.xml
file2.xml
文件不显示:
file1.backup.xml
file2.backup。 xml
我想问一下,如果可以像这样定义一个 QFileDialog
?
我相信你可以做的是:
$ b
- 创建一个自定义代理模型。您可以使用 QSortFilterProxyModel 作为您的模型的基类;
- 在代理模型中,覆盖 filterAcceptsRow 方法,并为其名称中包含。backup。字的文件返回false;
- 将新的代理模型设置为文件对话框: QFileDialog :: setProxyModel ;
下面是一个例子:
代理模式:
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;
//取消注释以调用默认实现
//返回QSortFilterProxyModel :: filterAcceptsRow(sourceRow,sourceParent);
$ p
$ b对话框是这样创建的:
QFileDialog对话框;
dialog.setProxyModel(new FileFilterProxyModel);
dialog.setNameFilter(XML(* .xml));
dialog.setOption(QFileDialog :: DontUseNativeDialog);
dialog.exec();
只有非本机文件对话框支持代理模式。
I would like to filter the files that are shown in a
QFileDialog
more specifically than just by file extensions. The examples I found in the Qt documentation only show filters likeImages (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)
and such. In addition to this I would also like to specify a filter for files that should not show up in the file dialog, e.g.XML files (*.xml)
but notBackup 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
?解决方案I believe what you can do is:
- Create a custom proxy model. You can use QSortFilterProxyModel as a base class for your model;
- In the proxy model override the filterAcceptsRow method and return false for files which have the ".backup." word in their names;
- Set new proxy model to the file dialog: 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.setProxyModel(new FileFilterProxyModel); dialog.setNameFilter("XML (*.xml)"); dialog.setOption(QFileDialog::DontUseNativeDialog); dialog.exec();
The proxy model is supported by non-native file dialogs only.
这篇关于在QFileDialog中过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!