Search code examples
pyqtpysidesignals-slotsundo-redoqstandarditemmodel

How to treat click events differently for an item's checkbox versus its text? (PyQt/PySide/Qt)


I have a QStandardItemModel in which each item is checkable. I want different slots to be called when I click on the item's checkbox, on one hand, versus when I click on its text, on the other. My ultimate goal is to have text edits, and changes in checkbox state, go onto the QUndoStack separately.

In my reimplementation of clicked I want to treat checkbox clicks and text clicks differently. So far, I have found no way to differentiate these events in the documentation for QCheckBox or QStandardItem. While QCheckBox has a toggled signal that I can use, I am not sure how to specifically listen for clicks on the text area.

I am trying to avoid having to set up coordinates manually and then listen for clicks in the different regions of the view of the item.

It doesn't seem this will be as simple as calling something like itemChanged, because that only gives you the new state of the item, not the previous state. Based on previous questions, I believe you need some way to pack the previous state into the undo stack, so you know what to revert to. That's what I am aiming to do with clicked, but there might be a better way.

This question piggybacks on the previous two in this series, in which I'm trying to figure out how to undo things in models:


Solution

  • Based on ekhumoro's suggestion and code nuggets, I built a treeview of a QStandardItemModel that emits a custom signal when an item changes. The code differentiates the text versus the checkbox changing via the role in setData (for text, use Qt.EditRole and for checkbox state changes use Qt.CheckStateRole) :

    # -*- coding: utf-8 -*-
    
    from PySide import QtGui, QtCore
    import sys
    
    class CommandTextEdit(QtGui.QUndoCommand):
        def __init__(self, tree, item, oldText, newText, description):
            QtGui.QUndoCommand.__init__(self, description)
            self.item = item
            self.tree = tree
            self.oldText = oldText
            self.newText = newText
    
        def redo(self):      
            self.item.model().itemDataChanged.disconnect(self.tree.itemDataChangedSlot) 
            self.item.setText(self.newText)
            self.item.model().itemDataChanged.connect(self.tree.itemDataChangedSlot) 
    
        def undo(self):
            self.item.model().itemDataChanged.disconnect(self.tree.itemDataChangedSlot) 
            self.item.setText(self.oldText)
            self.item.model().itemDataChanged.connect(self.tree.itemDataChangedSlot) 
    
    
    class CommandCheckStateChange(QtGui.QUndoCommand):
        def __init__(self, tree, item, oldCheckState, newCheckState, description):
            QtGui.QUndoCommand.__init__(self, description)
            self.item = item
            self.tree = tree
            self.oldCheckState = QtCore.Qt.Unchecked if oldCheckState == 0 else QtCore.Qt.Checked
            self.newCheckState = QtCore.Qt.Checked if oldCheckState == 0 else QtCore.Qt.Unchecked
    
        def redo(self): #disoconnect to avoid recursive loop b/w signal-slot
            self.item.model().itemDataChanged.disconnect(self.tree.itemDataChangedSlot) 
            self.item.setCheckState(self.newCheckState)
            self.item.model().itemDataChanged.connect(self.tree.itemDataChangedSlot) 
    
        def undo(self):
            self.item.model().itemDataChanged.disconnect(self.tree.itemDataChangedSlot)
            self.item.setCheckState(self.oldCheckState)
            self.item.model().itemDataChanged.connect(self.tree.itemDataChangedSlot) 
    
    
    class StandardItemModel(QtGui.QStandardItemModel):
        itemDataChanged = QtCore.Signal(object, object, object, object)
    
    
    class StandardItem(QtGui.QStandardItem):
        def setData(self, newValue, role=QtCore.Qt.UserRole + 1):
            if role == QtCore.Qt.EditRole:
                oldValue = self.data(role)
                QtGui.QStandardItem.setData(self, newValue, role)
                model = self.model()
                #only emit signal if newvalue is different from old
                if model is not None and oldValue != newValue:
                    model.itemDataChanged.emit(self, oldValue, newValue, role)
                return True
            if role == QtCore.Qt.CheckStateRole:
                oldValue = self.data(role)
                QtGui.QStandardItem.setData(self, newValue, role)
                model = self.model()
                if model is not None and oldValue != newValue:
                    model.itemDataChanged.emit(self, oldValue, newValue, role)
                return True
            QtGui.QStandardItem.setData(self, newValue, role)
    
    
    class UndoableTree(QtGui.QWidget):
        def __init__(self, parent = None):
            QtGui.QWidget.__init__(self, parent = None)
            self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
            self.view = QtGui.QTreeView()
            self.model = self.createModel()
            self.view.setModel(self.model)
            self.view.expandAll()
            self.undoStack = QtGui.QUndoStack(self)
            undoView = QtGui.QUndoView(self.undoStack)
            buttonLayout = self.buttonSetup()
            mainLayout = QtGui.QHBoxLayout(self)
            mainLayout.addWidget(undoView)
            mainLayout.addWidget(self.view)
            mainLayout.addLayout(buttonLayout)
            self.setLayout(mainLayout)
            self.makeConnections()
    
        def makeConnections(self):
            self.model.itemDataChanged.connect(self.itemDataChangedSlot)
            self.quitButton.clicked.connect(self.close)
            self.undoButton.clicked.connect(self.undoStack.undo)
            self.redoButton.clicked.connect(self.undoStack.redo)
    
        def itemDataChangedSlot(self, item, oldValue, newValue, role):
            if role == QtCore.Qt.EditRole:
                command = CommandTextEdit(self, item, oldValue, newValue,
                    "Text changed from '{0}' to '{1}'".format(oldValue, newValue))
                self.undoStack.push(command)
                return True
            if role == QtCore.Qt.CheckStateRole:
                command = CommandCheckStateChange(self, item, oldValue, newValue, 
                    "CheckState changed from '{0}' to '{1}'".format(oldValue, newValue))
                self.undoStack.push(command)
                return True
    
        def buttonSetup(self):
            self.undoButton = QtGui.QPushButton("Undo")
            self.redoButton = QtGui.QPushButton("Redo")
            self.quitButton = QtGui.QPushButton("Quit")
            buttonLayout = QtGui.QVBoxLayout()
            buttonLayout.addStretch()
            buttonLayout.addWidget(self.undoButton)
            buttonLayout.addWidget(self.redoButton)
            buttonLayout.addStretch()
            buttonLayout.addWidget(self.quitButton)
            return buttonLayout
    
        def createModel(self):
            model = StandardItemModel()
            model.setHorizontalHeaderLabels(['Titles', 'Summaries'])
            rootItem = model.invisibleRootItem()
            item0 = [StandardItem('Title0'), StandardItem('Summary0')]
            item00 = [StandardItem('Title00'), StandardItem('Summary00')]
            item01 = [StandardItem('Title01'), StandardItem('Summary01')]
            item0[0].setCheckable(True)
            item00[0].setCheckable(True)
            item01[0].setCheckable(True)
            rootItem.appendRow(item0)
            item0[0].appendRow(item00)
            item0[0].appendRow(item01)
            return model
    
    
    def main():
        app = QtGui.QApplication(sys.argv)
        newTree = UndoableTree()
        newTree.show()
        sys.exit(app.exec_())    
    
    if __name__ == "__main__":
        main()