Search code examples
pythonpyqtpysidesignals-slotsqtreewidget

Signal a QTreeWidgetItem toggled checkbox


How do I check if a checkbox is checked or unchecked? (In QTreeWidget) shows how to get the status of the checkbox on a qtreewidget item. Using that method, with a signal for itemClicked in the Tree, I can query if the selected item is checked or not. However, I then need to keep track of this item to see if it was previously checked or not.

Is there a method for knowing that a QTreeWidgetItem, which may be at different levels from the parent items, has a toggled checkbox, without making a subclass for it?

As a note, is there a specific reason the toggled method is not attached to the QTreeWidgetItem in QT?


Solution

  • The itemClicked signal is not a good choice for handling treewidget checkboxes.

    On the one hand, it gives false positives when not clicking on the checkbox part of an item; and on the other hand, it gives false negatives when the checkbox is toggled using the keyboard.

    I think the best that can be done with the existing signals, is to use itemChanged. This will register all checkbox state changes made with both keyboard and mouse. It is not the perfect general solution, though, because it will give false positives whenever any other item data is changed (e.g. text, font, background colour, etc). So you would need to block the itemChanged signal whenever those other types of changes were made.