c++qtqtreewidgetitem

QTreeWidgetItem find child by text


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


Solution

  • 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