一,不多说直接上代码

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);

最新文章

  1. Android WebView使用
  2. Ubuntu 14 中,SecureCRT、SecureFX个性化设置
  3. httpclient+Jsoup总结
  4. 【HTML5】拖放(Drag 和 drop)
  5. ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A Simple Job
  6. 单点登录 关于Cookie跨域的问题
  7. 【转】Dancing Links题集
  8. cocos2d-x 3.0 final 中文显示
  9. 【精解】EOS智能合约演练
  10. iOS平台添加Google Admob -1/2(Unity3D开发之七)
  11. Unity 2D 效应器与来回移动的实现
  12. 《OpenCV3编程入门》学习笔记
  13. 一、linux扩展
  14. 洛谷P3167 通配符匹配 [CQOI2014] 字符串
  15. STL next_permutation 算法原理和实现
  16. SQL优化原则(转)
  17. Android编程心得-在Assets文件夹中放入.sql文件实现创建SQlite表的操作
  18. cefglue Flash
  19. Singer 学习五 docker 运行说明
  20. maven正确的集成命令-U -B 等

热门文章

  1. JavaWeb_初识过滤器Filter
  2. 安装python第三方模块
  3. C#程序模拟登录批量获取各种邮件内容信息
  4. C++入门经典-例6.18-数组的动态分配,动态获得斐波那契数列
  5. C++入门经典-例5.1-输出变量的指针
  6. go.js-拖拽流程图插件
  7. 一次性生产KEY
  8. ffmpeg循环推流
  9. SpringBoot深入了解
  10. Win7、win8、win10下实现精准截获Explorer拷贝行为