Question

Using:

    item=QtGui.QTreeWidgetItem()
    item.setCheckState(0,QtCore.Qt.Unchecked)

the TreeWidget Item was set to display a checkbox. I would appreciate if you show me how to connect an Item's checkbox-state-change to a function (so I could link a function every time an Item's checkbox's state changed). And I would like to know what Item's attribute should be queried in order to get checkbox current state. So far I've been using QTreeWidget's

.itemChanged.connect(self.myFunction)

but all I get is Item object itself. What Item's property could be used to get its checkbox's status?...

Was it helpful?

Solution

looks like I faced your problem once upon a time and used this solution by ekhumoro:

def handle(self, item, column):
    self.treeWidget.blockSignals(True)
    if item.checkState(column) == QtCore.Qt.Checked:
        self.handleChecked(item, column)
    elif item.checkState(column) == QtCore.Qt.Unchecked:
        self.handleUnchecked(item, column)
    self.treeWidget.blockSignals(False)

class TreeWidgetItem(QtGui.QTreeWidgetItem):
    def setData(self, column, role, value):
        state = self.checkState(column)
        QtGui.QTreeWidgetItem.setData(self, column, role, value)
        if (role == QtCore.Qt.CheckStateRole and
            state != self.checkState(column)):
            treewidget = self.treeWidget()
            if treewidget is not None:
                treewidget.itemChecked.emit(self, column)

class Window(QtGui.QTreeWidget):
    itemChecked = QtCore.pyqtSignal(object, int)

    def __init__(self, rows, columns):
        QtGui.QTreeWidget.__init__(self)
        self.itemChecked.connect(self.handleItemChecked)

    def handleItemChecked(self, item, column):
        print 'ItemChecked', int(item.checkState(column))

Worked quite well for me :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top