QTreeWidgetItem find child by text

user3514538 picture user3514538 · Apr 17, 2015 · Viewed 10.7k times · Source

How to find item in QTreeWidgetItem by text? Is there an analog of QTreeWidget's findItem method ?

Answer

Nithish picture Nithish · Apr 17, 2015

I believe what you are looking for is recursive search in a QTreeWidget. For that you will have to use the combination of Qt::MatchContains | Qt::MatchRecursive as flag.

So if pMyTreeWidget is the pointer to your QTreeWidget and myText is the QString containing the text you want to search, assuming that the search has to be on column 0, the code will look something like:

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

If your requirement is to match the exact text, then you can use Qt::MatchExactly|Qt::MatchRecursive instead of Qt::MatchContains|Qt::MatchRecursive