Skip to content Skip to sidebar Skip to footer

How Do I Check If A Checkbox Is Checked Or Unchecked? (In QTreeWidget)

I have written the code below: from PyQt4 import QtCore, QtGui import sys class window(QtGui.QMainWindow): def __init__(self, parent=None): super(window, self).__init_

Solution 1:

Using itemClicked signal:

class window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(window, self).__init__(parent)
        self.TreeWidget = QtGui.QTreeWidget()
        self.TreeWidget.setColumnCount(1)
        # self.item1/2/3 = ....  save reference to the items
        #   to access them in the callback (check_status)
        item1 = self.item1 = QtGui.QTreeWidgetItem(["Item 1"])
        item1.setCheckState(0, QtCore.Qt.Checked)
        item2 = self.item2 = QtGui.QTreeWidgetItem(["Item 2"])
        item2.setCheckState(0, QtCore.Qt.Unchecked)
        item3 = self.item3 = QtGui.QTreeWidgetItem(["Item 3"])
        item3.setCheckState(0, QtCore.Qt.Unchecked)
        self.TreeWidget.addTopLevelItem(item1)
        self.TreeWidget.addTopLevelItem(item2)
        self.TreeWidget.addTopLevelItem(item3)
        self.setCentralWidget(self.TreeWidget)

        # connect the itemClicked signal to the callback check_status.
        self.TreeWidget.itemClicked.connect(self.check_status)

    def check_status(self):
        if self.item1.checkState(0) == QtCore.Qt.Checked:
            print('item 1 is checked')
        if self.item2.checkState(0) == QtCore.Qt.Checked:
            print('item 2 is checked')
        if self.item3.checkState(0) == QtCore.Qt.Checked:
            print('item 3 is checked')

Solution 2:

A checkbox is a kind of value in the field. It is different from the text, but still the value. The checkbox can be triggered with a mouse event or with a keyboard event (space bar). Therefore, the simplest way to recognize such an event is to subclass QTreeWidgetItem and store the last checkbox state in it as a variable of type QtCore.Qt.CheckState.

Please, consider the following piece of code:

from __future__ import annotations
from PyQt5.QtWidgets import *
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
import sys, typing

class MyTree(QTreeWidget):

    class MyItem(QTreeWidgetItem):
        def __init__(self, parent):
            super().__init__(parent)
            # Assuming, that an element has only one checkbox
            # 
            self.storedCheckedState: QtCore.Qt.CheckState = self.checkState(0)
            # Otherwise, we need more stored variables here

        def setCheckState(self, column: int, state: QtCore.Qt.CheckState) -> None:
            # The chekbox in the first column:
            if column == 0:
                self.storedCheckedState = state
            return super().setCheckState(column, state)

    def __init__(self, parent: typing.Optional[QWidget]=None) -> None:
        super().__init__(parent=parent)
        self.setColumnCount(4)
        header = MyTree.headerItem(self)
        header.setText(1, 'A')
        header.setText(2, 'B')
        header.setText(3, 'C')
        header.setText(4, 'D')

    def changed(self, item:MyTree.MyItem, column:int) -> None:
        if column == 0:
            if item.storedCheckedState != item.checkState(0):
                print("Item checked!")
                item.storedCheckedState = item.checkState(0)
            else:
                print("Item edited, but not checked!")
        else:
            print("Some field edited...")
        


    def loadData(self, data:typing.Any) -> None:
        # stop events propagation
        try:
            self.disconnect()
        except:
            pass
        # Load the data there...
        # In this particular case, 
        # the tree will be simply generated:
        # 
        # [ ] Parent 1
        #     [ ] Child 1
        #     [ ] Child 2
        #     [ ] Child 3
        # ...
        for i in range(3):
            # parent = QTreeWidgetItem(tree)
            parent = MyTree.MyItem(self)
            parent.setText(0, "Parent {}".format(i))
            parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable | Qt.TextEditable | Qt.ItemIsEditable)
            for x in range(5):
                child = MyTree.MyItem(parent)
                child.setFlags(child.flags() | Qt.ItemIsUserCheckable | Qt.TextEditable | Qt.ItemIsEditable)
                child.setText(0, "Child {}".format(x))
                child.setCheckState(0, Qt.Unchecked)

        # restore events propagation
        self.itemChanged.connect(self.changed)


def main(): 
    app = QApplication (sys.argv)
    tree = MyTree()    
    tree.loadData("ANY DATA")
    tree.show() 
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Post a Comment for "How Do I Check If A Checkbox Is Checked Or Unchecked? (In QTreeWidget)"