I have a simple tree with at most 30 items and at most a depth of 2. I am trying to find a specific item in the tree that is in the second column (which is hidden from display). When I search for a string I can guarantee it in the tree, I still get a list of nothing.
I am obviously misunderstanding the documentation. The columnCount()
for the tree is 1, so there is no second column from the tree’s perspective, which is why the search fails.
The QTreeWidgetItem documentation states “The given list of strings will be set as the item text for each column in the item.” So, I assume the item itself has multiple columns that has nothing at all to do with the tree’s columns.
I am really just trying to add a hidden, known id for each item, specifically for searching so the text can change if needed, such that find and load functionality isn’t dependent on the text or the index (as various sort options are also allowed in the real code). Sorry, new to trees.
MWE:
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QDialog, QVBoxLayout, QLabel
import sys
app = QApplication([])
main = QDialog()
main.setLayout(QVBoxLayout())
tree = QTreeWidget()
QTreeWidgetItem(tree, ['Test', 'test'])
main.layout().addWidget(tree)
label = QLabel()
main.layout().addWidget(label)
found = tree.findItems('test', Qt.MatchFlag.MatchRecursive | Qt.MatchFlag.MatchFixedString, 1)
label.setText(f'Found {len(found)} item(s)')
main.show()
sys.exit(app.exec())