QTreeWidgetItem 通过文本查找子项

QTreeWidgetItem find child by text

本文关键字:查找 文本 QTreeWidgetItem      更新时间:2023-10-16

如何在QTreeWidgetitem中按文本查找项目?有没有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