/**
* @brief 查找与txt相同的item
* @date 2020-11-10 10:56:33
* @param txt 要查找的文本
*/
void AddVistors::FindItem(const QString& txt)
{
ui->treeWidget->collapseAll();
if (txt.isEmpty()) {
QTreeWidgetItemIterator it(ui->treeWidget);
while (*it) {
(*it)->setForeground(0, Qt::black);
it++;
}
return;
}
if (!ui->treeWidget->topLevelItemCount()) {
return;
}
QTreeWidgetItemIterator it(ui->treeWidget);
while (*it) {
this->setCursor(Qt::WaitCursor);
QString str = (*it)->text(0);
if (-1 != str.indexOf(txt)) { // 精准匹配
QTreeWidgetItem* pItem = (*it)->parent();
if (pItem != nullptr) {
bool isExpanded = pItem->isExpanded();
if (!isExpanded) {
ui->treeWidget->expandItem(pItem);
}
}
(*it)->setForeground(0, Qt::red);
ParentExpand(*it);
} else {
(*it)->setForeground(0, Qt::black);
}
it++;
}
this->setCursor(Qt::ArrowCursor);
}
/**
* @brief 递归展开指定的节点的parent
* @date 2020-11-10 10:58:27
* @param item 树节点
*/
void AddVistors::ParentExpand(QTreeWidgetItem* item)
{
if (item->parent() != nullptr) {
QTreeWidgetItem* pItem = item->parent();
if (!pItem->isExpanded()) {
pItem->setExpanded(true);
}
ParentExpand(pItem);
}
}