QSortFilterProxyModel过滤QTreeView中的文件
来源:互联网 发布:软件著作权和专利 编辑:程序博客网 时间:2024/05/16 16:05
程序是用QTreeView显示QFileSystemModel的内容,通过QSortFilterProxyModel实现过滤。
pLocalViewModel = new(std::nothrow) QFileSystemModel();
if (NULL == pLocalViewModel)
{
Q_ASSERT(false);
return false;
}
pLocalProxyViewModel = new(std::nothrow) MySortFilterProxyModel();
if (NULL == pLocalProxyViewModel)
{
Q_ASSERT(false);
return false;
}
pLocalProxyViewModel->setSourceModel(pLocalViewModel);
pLocalProxyViewModel->setFilterKeyColumn(0);
pLocalProxyViewModel->setDynamicSortFilter(true);
// pLocalTreeView config
pLocalTreeView = new(std::nothrow) QTreeView();
if (NULL == pLocalTreeView)
{
Q_ASSERT(false);
return false;
}
pLocalTreeView->setModel(pLocalProxyViewModel);
通过QLineEdit输入关键字做过滤,当QLineEdit有字符变化时,就会触发过滤。
connect(filterLineEdit,SIGNAL(textChanged(const QString &)), this, SLOT(applyFilter(const QString&)));
void TPFileManagerPanel::applyFilter(constQString &text)
{
QRegExp regExp(text,Qt::CaseInsensitive, QRegExp::Wildcard);
if (NULL != pCurrentProxyModel && NULL != pCurrentTreeView)
{
pCurrentProxyModel->setFilterRegExp(regExp);
}
}
对于QTreeView中已经展开的节点,要求能过滤出所有满足条件的节点。如果子节点满足,父节点需要显示;如果没有子节点满足,父节点不显示。
参考< QSortFilterProxyModel实现QTreeView的过滤的缺点和改进>这篇博客,http://blog.csdn.net/lutx/article/details/7161467;
实现自己的MySortFilterProxyModel。发现当没有子项能够满足条件时,此时逐字符清空QLineEdit, QTreeView直接显示系统的根目录,且无法再变回原设定的rootIndex。
针对上述情况,做了以下改进,
boolMySortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex&source_parent) const
{
bool bRet = isParentRoot(source_parent);
if (bRet)
{
bool filter = QSortFilterProxyModel::filterAcceptsRow(source_row,source_parent);
if (filter)
{
return true;
}
else
{
// check all decendant's
QModelIndex source_index =sourceModel()->index(source_row, 0, source_parent);
for (int k=0;k<sourceModel()->rowCount(source_index); k++)
{
if (filterAcceptsRow(k,source_index))
{
return true;
}
}
}
return false;
}
return true;
}
boolMySortFilterProxyModel::isParentRoot(const QModelIndex &index) const
{
QFileSystemModel *sm =qobject_cast<QFileSystemModel*>(sourceModel());
if (index.isValid())
{
if (index == sm->index(sm->rootPath()))
{
return true;
}
else
{
returnisParentRoot(index.parent());
}
}
return false;
}
经过验证,此时不论过滤条件如何改变,QTreeView不再显示错误。
- QSortFilterProxyModel过滤QTreeView中的文件
- QSortFilterProxyModel实现QTreeView的过滤的缺点和改进
- QT的排序与过滤(QSortFilterProxyModel)
- QSortFilterProxyModel
- QSortFilterProxyModel
- QSortFilterProxyModel
- 自定义QTreeView中的条目编辑器
- QStandardItemModel在QTreeView中的使用
- qtreeview
- QTreeView
- QTreeView
- 文件过滤驱动中的重入处理
- 文件过滤驱动中的重入处理
- wince中的文件过滤驱动(FSDSPY)
- CFileDlg 中的文件过滤字符串写法
- 过滤XML文件中的无效字符
- 文件过滤驱动中的重入处理
- 过滤txt文件中的一些行
- hdu1175: 连连看
- 嵌入式 linux中dd命令[用于制作U盘启动盘的神奇的命令]
- poj 1006
- 今天开通CSDN博客,写写感想和计划
- Struts2返回JSON数据的具体应用范例
- QSortFilterProxyModel过滤QTreeView中的文件
- javascript函数也是一个对象
- 基于axis2的基本java对象webservice发布
- 黑客与画家 书评
- 模板(template)中typename的使用方法
- ExtJs--01--引入ExtJs4.1.1
- 易语言 GetWindowRect 不能返回非基本数据类型数据。
- 在IOS 7中使用UITableViewController
- 怎样将CString 转换到string