dimanche 11 septembre 2022

PySide6 QTreeWidget Checkbox SingleSelection

have a PySide6 QTreeWidget with some Elements and Checkboxes, very simple. What i cannot getting to work is how can i make s Single Selection with the Checkboxes? What works is SingleSelection without the Checkboxes, but not when i only use the Checkboxes itself. I wann use only Checkboxes and not Mouseclicks on the row, i did that with

tv.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
tv.setFocusPolicy(QtCore.Qt.NoFocus)

My Idea was set a itemChanged handler function for it and when a checkbox is clicked i run through all items, set all item checkboxes to uncheck with

child.setCheckState(0, QtCore.Qt.Unchecked)

and after that set the item that is selected from the handler to checked.

def handle_statechange(item):
    selected_item = item.text(0)
    for i in range(tv.invisibleRootItem().childCount()):
        child = tv.invisibleRootItem().child(i)
        child.setCheckState(0, QtCore.Qt.Unchecked)
    item.setCheckState(0, QtCore.Qt.Checked)

But that doesnt work. How can i make this behavior? Thanks!




Aucun commentaire:

Enregistrer un commentaire