How to implement itemChecked and itemUnchecked signals for QTreeWidget in PyQt4?

前端 未结 2 862
遇见更好的自我
遇见更好的自我 2020-12-18 04:46

Where are the signals itemChecked and itemUncheсked on the QTreeWidget?

Qt Signals: (quote from PyQt4 QTreeWidget documentation page)

void currentItemChange         


        
相关标签:
2条回答
  • 2020-12-18 05:18

    To complete the answer of @ekhumoro, and to give a shorter answer (without subclassing QTreeWidgetItem): block the signal :

    self.treeWidget.blockSignals(True)
    self.treeWidget.blockSignals(False)
    

    and use

    self.treeWidget.itemChanged
    

    to connect to what you want. the signal is used when the data is changed (text inside, checked state, ...) and in your function linked to this signal verify if the treeWidget has changed is checked state.

    0 讨论(0)
  • 2020-12-18 05:38

    To avoid problems with recursion when using the itemChanged signal, try temporarily blocking signals until the handler has completed:

    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)
    

    UPDATE

    The other part of your question asked about emitting a signal only when an item is checked.

    One way to do this would be to subclass QTreeWidgetItem and reimplement it's setData function:

    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))
    
    0 讨论(0)
提交回复
热议问题