QTreeWidgetItem 通过文本查找子项

QTreeWidgetItem find child by text

如何通过文本查找QTreeWidgetItem中的item?是否有 QTreeWidget 的 findItem 方法的模拟?

我相信您正在寻找的是 QTreeWidget 中的递归搜索。为此,您必须使用 Qt::MatchContains | Qt::MatchRecursive 的组合作为标志。

因此,如果 pMyTreeWidget 是指向您的 QTreeWidget 的指针,而 myText 是包含您要搜索的文本的 QString,假设搜索必须在第 0 列,代码将看起来类似于:

QList<QTreeWidgetItem*> clist = pMyTreeWidget->findItems(myText, Qt::MatchContains|Qt::MatchRecursive, 0);
foreach(QTreeWidgetItem* item, clist)
{
    qDebug() << item->text(0);
}

如果您的要求是精确匹配文本,那么您可以使用Qt::MatchExactly|Qt::MatchRecursive代替Qt::MatchContains|Qt::MatchRecursive