Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

itemChanged() event not triggering on custom QListWidget?



  • Hello

    There appears to be something I'm not understanding about a custom widget inheriting QListWidget, making items checkable, and how to detect when a checkstate has changed.

    My test code is below, and it shows a form with 10 checkboxes in a CustomListWidget(QListWidget). When I check or uncheck any of the 10 items, I am expecting the update_display() function to get called. However, it never gets triggered.

    From other forums I've found the suggestion that 'itemChanged' instead of 'itemSelectionChanged' is the event to listen out for, but I've tried both, and neither seem to trigger.

    If possible please can somebody point me in the direction of how to properly detect a checkstate change event on this widget?

    (Obviously this code is just an example and once I get this working, there will be extra functions added to CustomListWidget to justify the sub-classing.)

    import sys
    from PyQt5.QtWidgets import QApplication, QDialog, QGridLayout, QStatusBar, QListWidget
    from PyQt5 import QtCore
    
    
    class CustomListWidget(QListWidget):
        selectionChanged = QtCore.pyqtSignal()
    
        def __init__(self, parent=None):
            QStatusBar.__init__(self, parent)
    
        def itemChanged(self, ev):
            self.selectionChanged.emit()
    
    
    class ListWidgetTest(QDialog):
        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 tests'
            self.layout = QGridLayout(self)
    
            self.customList = CustomListWidget()
            self.layout.addWidget(self.customList, 0, 0)
            for i in range(0, 10):
                self.customList.addItem(str(i))
                self.customList.item(i).setData(QtCore.Qt.WhatsThisRole, "Data" + str(i))
                self.customList.item(i).setFlags(self.customList.item(i).flags() | QtCore.Qt.ItemIsUserCheckable)
                self.customList.item(i).setCheckState(QtCore.Qt.Checked)
            self.customList.selectionChanged.connect(lambda: self.update_display())
    
            self.show()
    
        def update_display(self):
            print("Reached update_display, signal received, hooray.")
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = ListWidgetTest()
        sys.exit(app.exec_())
    
    


  • @donquibeats said in itemChanged() event not triggering on custom QListWidget?:
    I don't know whether this is your problem, but:

    class CustomListWidget(QListWidget):
    
        def __init__(self, parent=None):
            QStatusBar.__init__(self, parent)
    

    Deriving from QListWidget and having its __init__() call QStatusBar.__init__(self, parent). Is that right/safe/intended? You're not letting the QListWidget init itself, and instead you're initting a QStatusBar....

    If it is that, that's why I always go super().__init__() :)


Log in to reply