一,不多说直接上代码

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

qt treeview过滤的更多相关文章

  1. Qt之QStringList讲解

    QStringList类提供了一个字符串列表 从QString继承而来,它提供快速索引为基础的接入以及快速插入和清除. 成员函数用于操作这个字符串列表如: append(),insert(),repl ...

  2. Qt如何在QTabWidget上绘图

    Qt绘图事件必须在paintEvent事件下绘图,这样导致我们在向Qt设计师界面上拖拽的控件绘图时,造成了很大的麻烦. 我们不能在拖拽的控件上写paintEvent函数,但是可以自定义一个类,继承某一 ...

  3. QT事件过滤器(QT事件处理的5个层次:自己覆盖或过滤,父窗口过滤,Application过滤与通知)

    Qt事件模型一个真正强大的特色是一个QObject 的实例能够管理另一个QObject 实例的事件. 让我们试着设想已经有了一个CustomerInfoDialog的小部件.CustomerInfoD ...

  4. Qt中事件处理的方法(三种处理方法,四种覆盖event函数,notify函数,event过滤,事件处理器。然后继续传递给父窗口。可观察QWidget::event的源码,它是虚拟保护函数,可改写)

    一.Qt中事件处理的方式   1.事件处理模式一 首先是事件源产生事件,最后是事件处理器对这些事件进行处理.然而也许大家会问, Qt中有这么多类的事件,我们怎么样比较简便的处理每个事件呢?设想,如果是 ...

  5. Qt消息机制和事件、事件过滤

    一,事件 事件(event)是由系统或者 Qt 本身在不同的时刻发出的.当用户按下鼠标.敲下键盘,或者是窗口需要重新绘制的时候,都会发出一个相应的事件.一些事件在对用户操作做出响应时发出,如键盘事件等 ...

  6. Qt事件机制---信号通过事件实现,事件可以过滤,事件更底层,事件是基础,信号是扩展。

    转:http://www.cnblogs.com/findumars/p/8001484.html Qt事件机制(是动作发生后,一种通知对象的消息,是被动与主动的总和.先处理自己队列中的消息,然后再处 ...

  7. [Qt]自定义表头实现过滤功能

    1. 写在前面 过滤功能源自项目上交互优化用户体验,在表头添加过滤符号实现过滤,替换以往在表格上方占用一行过滤项进行过滤. 2. 过滤提示 过滤提示就是三态图标(normal,hover,press) ...

  8. Qt去掉treeview项的焦点虚线

    项目做到后期,进行局部美化的时候发现了问题,在treeview框选择状态下会有虚线. 其实,不仅是treeview,tableview,listview,乃至button在有焦点的情况下,都会出现虚线 ...

  9. qt qml Treeview使用记录--设置每个Item的图片logo,高度

    这篇帮助很大: https://blog.csdn.net/qq_32116695/article/details/81298585, 代码如下: TreeView { id: viewTree an ...

随机推荐

  1. windows管理员权限激活

    第一步:计算机-右键--管理--选择用户,选择administrator用户--取消勾选:账户禁用 第二步:alt+ctrl+delete,快捷键调出资源管理器--点击切换用户 第三步:显示出现adm ...

  2. 为Windows编译libobjc2(通过交叉编译的方式)

    前提:Linux系统.git.clang-8.g++-mingw-w64-x86-64.gobjc++-mingw-w64-x86-64. 一.下载源代码 git clone https://gith ...

  3. Running .sh scripts in Git bash

    Running .sh scripts in Git bash Let's say you have a script script.sh. To run it (using Git Bash), y ...

  4. GetRGB下载

    下载地址:https://pan.baidu.com/s/11EyUPa2WxhIgdsTRZtj_eg 07年6-8月做的,用于屏幕取色. 2019年8月30日13点50分

  5. Android中@id与@+id区别和sharedUserId属性详解

    Android中的组件需要用一个int类型的值来表示,这个值也就是组件标签中的id属性值. id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc.@+id/xyz等. 如果在@后 ...

  6. Retrofitting Analysis

    Retrofitting Analysis To figure out the process of retrofitting[1] objective updating, we do the fol ...

  7. LinuxGPIO中文文档

    本文来自Linux官方文档英文版,由于需要使用Linux的GPIO进行实验,我翻译了这篇文档. 本文档描述了GPIO框架的使用者接口.注意它描述了新的基于描述符的接口. 不推荐使用的基于整数的GPIO ...

  8. Ant Design使用方法

    1.antd官网: https://ant.design/docs/react/introduce-cn 2.React中使用Antd 1.安装antd npm install antd --save ...

  9. 阶段3 2.Spring_04.Spring的常用注解_7 改变作用范围以及和生命周期相关的注解

    Scope 改成多例 PreDestory和PostConstruct PreDestory和PostConstruct这两个注解了解即可 增加两个方法,分别用注解 没有执行销毁方法. 如果你一个子类 ...

  10. gcc编译器创建和使用静态库、动态库

    http://www.cnblogs.com/dyllove98/archive/2013/06/25/3155599.html 目录树结构: test/include/hello.h #ifdef ...