转:https://blog.csdn.net/qq_15821883/article/details/100096659?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight
void OnFindItem(const QString &qsFilter)
{
QTreeWidgetItemIterator it(ui->treeWidget);
while (*it)
{
//QTreeWidgetItem是否满足条件---这里的条件可以自己修改
if((*it)->text(0).contains(qsFilter))
{
(*it)->setHidden(false);
QTreeWidgetItem *item = *it;
//显示父节点
while (item->parent())
{
item->parent()->setHidden(false);
item = item->parent();
}
}
else
{
//不满足满足条件先隐藏,它的子项目满足条件时会再次让它显示
(*it)->setHidden(true);
}
++it;
}
}