QTreeview实现搜索功能查找功能

QTreeview查找功能的需求:

1.搜索某个节点时,该节点的父节点和子节点都显示出来

效果图如下:

 

输入搜索内容后:

 

核心代码如下:

bool TableViewSortFilterModelBase::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
//    if(!sourceParent.isValid())
//    {
//        return false;
//    }
    QAbstractItemModel *pModel = sourceModel();
    if(NULL == pModel)  return false;
    //--------------如果本节点的父节点可见,那么本节点也要可见
    {
        QModelIndex source_index = sourceParent;
        while (source_index.isValid())
        {
            bool isContains = pModel->data(source_index).toString().contains(filterRegExp());
            if(isContains)  return true;
 
            source_index = sourceModel()->parent(source_index);
        }
    }
    //--------------判断本身是否可见
    QModelIndex index;
    bool isContains = false;
    int columnCount = pModel->columnCount();
    for(int i=0;i<columnCount;i++)
    {
        index = pModel->index(sourceRow, i, sourceParent);
        if (index.column() != m_filterColumn && -1 != m_filterColumn)	continue;	//根据所选过滤类型选择需要进行过滤的列
 
        isContains = pModel->data(index).toString().contains(filterRegExp());
        if(isContains)  return true;
    }
    //--------------如果本节点的子节点可见,那么本节点也要可见
    {
        // check all decendant's
        QModelIndex source_index = sourceModel()->index(sourceRow, 0, sourceParent);
        if(!source_index.isValid())
        {
            return false;
        }
 
        for (int k=0; k<sourceModel()->rowCount(source_index); k++)
        {
            if (filterAcceptsRow(k, source_index))
            {
                return true;
            }
        }
    }
    return false;
}

 

源码下载地址:https://download.csdn.net/download/liuguangzhou123/11206060

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在QTreeView实现检索功能,可以通过以下步骤操作: 1. 获取要检索的字符串并将其转换为QRegularExpression对象。 2. 使用QTreeView的model()函数获取其模型。 3. 遍历模型中的所有项并使用QRegularExpression匹配项的文本。 4. 如果匹配,则将项设置为可见,否则将其设置为不可见。 以下是实现功能的示例代码: ```c++ QString searchString = "example"; // 检索的字符串 QRegularExpression regex(searchString, QRegularExpression::CaseInsensitiveOption); // 将字符串转换为QRegularExpression对象 QTreeView *treeView = new QTreeView(this); // 创建QTreeView对象 QStandardItemModel *model = new QStandardItemModel(this); // 创建QStandardItemModel对象并设置到QTreeView中 treeView->setModel(model); // 遍历模型并匹配项的文本 for (int row = 0; row < model->rowCount(); ++row) { QModelIndex parentIndex = model->index(row, 0); // 获取父索引 for (int column = 0; column < model->columnCount(parentIndex); ++column) { QModelIndex index = model->index(row, column, parentIndex); // 获取子索引 QString text = index.data(Qt::DisplayRole).toString(); // 获取项的文本 if (regex.match(text).hasMatch()) { treeView->setRowHidden(row, parentIndex, false); // 如果匹配,将项设置为可见 } else { treeView->setRowHidden(row, parentIndex, true); // 如果不匹配,将项设置为不可见 } } } ``` 在实际使用中,您可以将此代码放入到一个函数中,并将其与某个事件(例如按钮单击事件)相关联。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值