When I choose a selection in the QTreeWidget
programmatically using item_selected('Item2')
the selection gets passed to the handler as expected. I would also like that item to have a selection highlight, but I can't seem to figure that out. Any ideas?
from PyQt5.Qt import Qtfrom PyQt5.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItemimport sysdef item_selected(selection): try: print(selection.text(0)) except AttributeError: print(selection)app = QApplication(sys.argv)TreeList = ({'Header1': (('Item1', 'Item2', )),'Header2': (('Item11', 'Item21', )),})tree = QTreeWidget()for key, value in TreeList.items(): parent = QTreeWidgetItem(tree, [key]) for val in value: child = QTreeWidgetItem([val]) child.setFlags(child.flags() | Qt.ItemIsUserCheckable) child.setCheckState(0, Qt.Unchecked) parent.addChild(child)tree.itemClicked.connect(item_selected)tree.show()# SELECT AND HIGHLIGHT THIS ONEitem_selected('Item2')sys.exit(app.exec_())
Sorry if the above code is a mess.