How to find item in QTreeWidgetItem by text? Is there an analog of QTreeWidget's findItem method ?
Asked
Active
Viewed 1.5k times
8
-
Do you mean find item in the list of children of QTreeWidgetItem ? – Ashot Apr 17 '15 at 08:54
-
@Ashot, yes, I want to find item by text – Denis Apr 17 '15 at 08:59
-
2Seems like there is not analog method in QTreeWidgetItem. Anyway you can iterate over the children of item and find out what have text you want. – Ashot Apr 17 '15 at 09:34
-
@Ashot, I decided to do just that. Thanks for answer – Denis Apr 17 '15 at 09:46
1 Answers
16
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

Nithish
- 1,580
- 12
- 21