在Qt中只过滤QFileSystemModel中的dirs和xml文件 [英] Filtering only dirs and xml file in QFileSystemModel in Qt
问题描述
我已在
QListView
QTreeView
QFileSystemModel
我想要什么!
对话框,用于仅浏览xml文件。所以我想只显示dirs和xml文件在QListView
What i want !
a browsing dialog which use to browse xml file only. So i want to show dirs and xml files only in QListView
就像大多数浏览特定类型文件。如MSWord(只显示.doc和.docx浏览)
like in most of cases where you are browsing a specific type of file. as in MSWord ( show only .doc and .docx to browse)
m_ptrModelForTree = new QFileSystemModel(this);
m_ptrModelForTree->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
m_ptrModelForTree->setRootPath("");
ui->treeView->setModel(m_ptrModelForTree);
ui->treeView->hideColumn(1);
ui->treeView->hideColumn(2);
ui->treeView->hideColumn(3);
ui->treeView->header()->hide();
m_ptrModelForList = new QFileSystemModel(this);
m_ptrModelForList->setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
m_ptrModelForList->setRootPath("");
ui->listView->setModel(m_ptrModelForList);
ui->listView->setRootIndex(m_ptrModelForList->index("c:\\"));
我得到了
所有dirs和所有文件(但我只需要xml文件显示)
What i got
dialog which showing all dirs and all file ( but i need only xml file to display )
m_ptrModelForList->setNameFilters(QStringList()<<".xml");
但它只显示xml文件,而不是dirs。
but it showing xml file only, not dirs.
请给我建议做什么。
推荐答案
实际上,这是@Andreas在对问题的意见中建议的解决方案。
我的贡献指向在名称过滤器中的另一个错误。
Actually it's a solution suggested by @Andreas in comments to the question. My contribution is pointing to another mistake in the name filter.
解决方案:如何显示按扩展程序过滤的所有dirs +文件
-
使用标志QDir :: AllDirs。根据 docs ,此标记旨在避免应用过滤到文件夹。
Use flag QDir::AllDirs. According to docs this flag is intented to avoid applying the filter to folders.
setFilter(QDir :: Files | QDir :: AllDirs | QDir :: NoDotAndDotDot);
setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
使用 setNameFilters
为文件设置过滤器。过滤器是通配符。
您的错误,使用将其设置为.xml,这意味着文件不应该有其名称,但扩展名只能匹配您的过滤器。正确的过滤器为:
Use setNameFilters
to set a filter for files. The filters are wildcards.
Your mistake that use set it to ".xml" which means that a file shouldn't have its name but extension only to match your filter. The right filter is:
setNameFilters(QStringList()<* .xml)
setNameFilters(QStringList() << "*.xml")
这篇关于在Qt中只过滤QFileSystemModel中的dirs和xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!