QTreeWidgetItem按文本查找子项

use*_*538 4 c++ qt qtreewidgetitem

如何通过文本在QTreeWidgetItem中查找项目?是否有QTreeWidget的findItem方法的类比?

nnb*_*nnb 9

我相信你要找的是QTreeWidget中的递归搜索.为此,您将必须使用Qt::MatchContains | Qt::MatchRecursiveas标志的组合.

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

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

如果您的要求与确切的文本相匹配,那么您可以使用Qt::MatchExactly|Qt::MatchRecursive而不是Qt::MatchContains|Qt::MatchRecursive