qt treeview过滤
2024-09-04 00:37:51
一,不多说直接上代码
QSortFilterProxyModel可实现过滤排序。但是如果直接使用只能对于父项进行过滤
这里需要继承
头文件
#include <QSortFilterProxyModel>
class TreeProxyFilter:public QSortFilterProxyModel
{
public:
TreeProxyFilter(QObject *parent = Q_NULLPTR);
protected:
bool filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const;
private:
bool ShowThis(const QModelIndex index) const;
};
cpp文件 bool TreeProxyFilter::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
return ShowThis(index);
}
bool TreeProxyFilter::ShowThis(const QModelIndex index) const
{
bool retVal = false;
//Gives you the info for number of childs with a parent
if ( sourceModel()->rowCount(index) > 0 )
{
for( int nChild = 0; nChild < sourceModel()->rowCount(index); nChild++)
{
QModelIndex childIndex = sourceModel()->index(nChild,0,index);
if ( ! childIndex.isValid() )
break;
retVal = ShowThis(childIndex);
if (retVal)
{
break;
}
}
}
else
{
QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
QString type = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
if (!type.contains(filterRegExp()))
{
retVal = false;
}
else
{
retVal = true;
}
}
return retVal;
}
重写filterAcceptsRow方法,在该方法里对过滤项进行筛选。 二。使用
proxyModel = new TreeProxyFilter(this);
proxyModel->setSourceModel(model);
proxyModel->setFilterKeyColumn(0);
connect(ui->filterLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(factorFilter()));
ui->treeView->setModel(proxyModel);
最新文章
- Android WebView使用
- Ubuntu 14 中,SecureCRT、SecureFX个性化设置
- httpclient+Jsoup总结
- 【HTML5】拖放(Drag 和 drop)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A Simple Job
- 单点登录 关于Cookie跨域的问题
- 【转】Dancing Links题集
- cocos2d-x 3.0 final 中文显示
- 【精解】EOS智能合约演练
- iOS平台添加Google Admob -1/2(Unity3D开发之七)
- Unity 2D 效应器与来回移动的实现
- 《OpenCV3编程入门》学习笔记
- 一、linux扩展
- 洛谷P3167 通配符匹配 [CQOI2014] 字符串
- STL next_permutation 算法原理和实现
- SQL优化原则(转)
- Android编程心得-在Assets文件夹中放入.sql文件实现创建SQlite表的操作
- cefglue Flash
- Singer 学习五 docker 运行说明
- maven正确的集成命令-U -B 等