QtreeWidget实现模糊搜索功能

在输入框输入检索关键字,然后根据输入的关键字去搜索任务列表,只显示包含检索关键字的项随着输入文字的变化,不断的刷新搜索内容;效果如下所示。

 

 

搜索函数实现方法如下所示:

void TreeTaskList::SlotFilterChanged(QString strText)
{
    QTreeWidgetItemIterator it(ui.treeWidget);
    QString str = ui.SearchInput->text();
    //为空时全部显示
    if (str.isEmpty()) 
    {
        while (*it) 
        {
            (*it)->setHidden(false);
            //添加搜索关键字,高亮显示
            TreeItemBase* base =(TreeItemBase*) ui.treeWidget->itemWidget((*it),0);
            if (base)
            {
                base->SetFilterHover(strText);
            }
            ++it;
        }
        SlotUpdateTreeListBackground();
        return;
    }
    //不为空时,先全部隐藏
    while (*it) {
        (*it)->setHidden(true);
        ++it;
    }
    //搜索名字包含
    QList<QTreeWidgetItem*> items;
    int count = ui.treeWidget->topLevelItemCount();
    QTreeWidgetItem* topitem = NULL;
    for (int i = 0; i < count; i++)
    {
        topitem = ui.treeWidget->topLevelItem(i);
        if (topitem)
        {
            QVariantMap info = topitem->data(0, 256).toMap();
            if (info.value("type").toString() == "-1")
            {
                continue;
            }
            RecursiveTasksStatistic(topitem, items, strText);
            if (info.value("name").toString().contains(strText, Qt::CaseInsensitive))
            {
                items.push_back(topitem);
            }
        }
        
    }
    if (items.size()== 0)
    {
        ui.treeWidget->setStyleSheet(QLatin1String("QTreeWidget\n"
            "{\n"
            "   background: #303033;\n"
            "   font-size:16px;\n"
            "   color: rgba(255,255,255,0);\n"
            "    border:0px;\n"
            "     background-image:url(:/analysisCenter/Resources/analysisCenter/searchEmpty.png);\n"
            "    background-repeat:no-repeat;\n"
            "    background-position:center;\n"
            "}\n"
            "\n"
            "QTreeWidget::item\n"
            "{\n"
            "    background: #303033;\n"
            "}\n"
            "QTreeWidget::branch\n"
            "{\n"
            "    background:#303033;\n"
            "}\n"
            "QTreeWidget::item:hover \n"
            "{\n"
            "    background: rgba(255,255,255,0.12); \n"
            "\n"
            "}\n"
            "\n"
            "QTreeWidget::item:selected{\n"
            "\n"
            "   background: rgba(32,128,247,0.32);\n"
            "    color: rgba(32,128,247,0);\n"
            "}\n"
            "QTreeWidget::branch:closed:has-children:!has-siblings,\n"
            "QTreeWidget::branch:closed:has-children:has-siblings {\n"
            "   background-image:url(:/analysisCenter/Resources/analysisCenter/arrowexpend-nor.svg);\n"
            "    background-repeat:no-repeat;\n"
            "    background-position:center;\n"
            "}\n"
            "\n"
            "QTreeWidget::branch:open:has-children:!has-siblings,\n"
            "QTreeWidget::branch:open:has-children:has-siblings  {\n"
            "   back"
            "ground-image:url(:/analysisCenter/Resources/analysisCenter/arrowclose-nor.svg);\n"
            "    background-repeat:no-repeat;\n"
            "    background-position:center;\n"
            "}"));
    }
    else
    {
        ui.treeWidget->setStyleSheet(QLatin1String("QTreeWidget\n"
            "{\n"
            "   background: #303033;\n"
            "   font-size:16px;\n"
            "   color: rgba(255,255,255,0);\n"
            "    border:0px;\n"
            "}\n"
            "\n"
            "QTreeWidget::item\n"
            "{\n"
            "    background: #303033;\n"
            "}\n"
            "QTreeWidget::branch\n"
            "{\n"
            "    background:#303033;\n"
            "}\n"
            "QTreeWidget::item:hover \n"
            "{\n"
            "    background: rgba(255,255,255,0.12); \n"
            "\n"
            "}\n"
            "\n"
            "QTreeWidget::item:selected{\n"
            "\n"
            "   background: rgba(32,128,247,0.32);\n"
            "    color: rgba(32,128,247,0);\n"
            "}\n"
            "QTreeWidget::branch:closed:has-children:!has-siblings,\n"
            "QTreeWidget::branch:closed:has-children:has-siblings {\n"
            "   background-image:url(:/analysisCenter/Resources/analysisCenter/arrowexpend-nor.svg);\n"
            "    background-repeat:no-repeat;\n"
            "    background-position:center;\n"
            "}\n"
            "\n"
            "QTreeWidget::branch:open:has-children:!has-siblings,\n"
            "QTreeWidget::branch:open:has-children:has-siblings  {\n"
            "   back"
            "ground-image:url(:/analysisCenter/Resources/analysisCenter/arrowclose-nor.svg);\n"
            "    background-repeat:no-repeat;\n"
            "    background-position:center;\n"
            "}"));
    }
    foreach(auto var, items) {
        var->setHidden(false);
        TreeItemBase* base = (TreeItemBase*)ui.treeWidget->itemWidget(var, 0);
        if (base)
        {
            base->SetFilterHover(strText);
        }
        while (var->parent()) {
            var->parent()->setHidden(false);//显示
            var->parent()->setExpanded(true);//展开
            var = var->parent();
        }
    }


}

 

采用递归的方法实现去获取treeWidget中每一个item的名称,是否包含了检索关键字;

void TreeTaskList::RecursiveTasksStatistic(QTreeWidgetItem* pCurrentItem,QList<QTreeWidgetItem*>& searchItem, QString strContent)
{
    if (pCurrentItem==NULL||strContent=="")
    {
        return;
    }
    QTreeWidgetItem* pFindTask = NULL;
    int count = pCurrentItem->childCount();
    QTreeWidgetItem* child = NULL;
    QString strName = "";
    QVariantMap datainfo;
    for (int i = 0; i < count; i++)
    {
        child = pCurrentItem->child(i);
        datainfo = child->data(0, 256).toMap();
        strName = datainfo.value("name").toString();
        if (strName.contains(strContent, Qt::CaseInsensitive))
        {
            searchItem.push_back(child);
        }
        if (datainfo.value("type").toString() == "1"|| datainfo.value("type").toString() == "0")//子文件夹或者
        {
            RecursiveTasksStatistic(child, searchItem, strContent);
        }
    }
}

 

posted @ 2023-02-27 21:24  一字千金  阅读(542)  评论(0编辑  收藏  举报