QT TreeView自定义Model 实现自定义过滤筛选数据

简介

通常我们使用QSortFilterProxyModel过滤数据时,只会过滤树的父节点,符合条件的子节点不会被显示出来,因此我们需要继承QSortFilterProxyModel,重写filterAcceptsRow函数可以实现符合条件的子节点也显示出来。
.h

#include <QSortFilterProxyModel>

class SelectFileSortFilterProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT
public:

    explicit SelectFileSortFilterProxyModel(QObject *parent = nullptr);
    ~SelectFileSortFilterProxyModel() = default;

protected:
    bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;

};

.cpp

#include "SelectFileSortFilterProxyModel.h"

SelectFileSortFilterProxyModel::SelectFileSortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
{

}

bool SelectFileSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
    bool relust = QSortFilterProxyModel::filterAcceptsRow(source_row,source_parent);
    if(relust)
    {   //本来就符合条件直接返回
        return true;
    }
    else
    {   //若节点不符合判断条件,判断其下是否有子节点,若有则需要显示
    //可根据需求自行修改下面的判断内容
        QModelIndex source_index = sourceModel()->index(source_row, 0, source_parent);
        if(sourceModel()->rowCount(source_index)>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    return relust;
}

用法:

//新建排序model
m_sortmodel = new SelectFileSortFilterProxyModel(this);
//将model设置到treeview中
ui->treeView->setModel(m_sortmodel);
//新建数据model
m_model = new SelectFileModel;
//将数据model设置到过滤model中
m_sortmodel->setSourceModel(m_model);

效果:
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要自定义C++ model 实现 QML 的 TreeView,需要遵循以下步骤: 1. 创建一个 C++ 类,继承自 QAbstractItemModel。 2. 实现必要的虚函数,包括 rowCount、columnCount、parent、index、data 等。 3. 在 model 中创建树形结构。 4. 在 QML 中创建 TreeView 控件,并绑定数据源为自定义的 C++ model。 5. 为 TreeView 控件定义 itemDelegate,以便在 TreeView 中显示自定义数据。 以下是一个简单的示例: C++ model: ```c++ class TreeModel : public QAbstractItemModel { Q_OBJECT public: explicit TreeModel(QObject *parent = nullptr); ~TreeModel(); // 实现必要的虚函数 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &child) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; private: // 存储树形结构的数据 QList<TreeItem*> m_items; }; ``` QML TreeView: ```qml TreeView { model: myModel itemDelegate: Rectangle { width: 200 height: 30 Text { text: model.name // 自定义数据 anchors.centerIn: parent } } } ``` 在 TreeModel 中,需要实现一个 TreeItem 类来存储树形结构的数据。在 data 函数中,可以根据需要返回不同的数据,例如 name、icon 等。在 QML 中,可以通过 itemDelegate 自定义每个 item 的显示方式。 需要注意的是,自定义的 C++ model 在 QML 中使用时需要在 main 函数中注册。可以使用 qmlRegisterType 函数将自定义的 C++ 类注册到 QML 中,例如: ```c++ qmlRegisterType<TreeModel>("MyModel", 1, 0, "TreeModel"); ``` 这样就可以在 QML 中使用 TreeModel 作为数据源了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值