Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1512

Non-deterministic bug when setting flags and checkState on QStandardItem

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Incomplete
    • Affects Version/s: 5.14.2.2
    • Fix Version/s: None
    • Component/s: PySide
    • Labels:
      None
    • Environment:
      This was being run on Windows 10.
    • Platform/s:
      Windows

      Description

      This bug was found when trying to create a custom comboBox that has checkboxes. Below is the following code:

       

      Example code

       
      class CheckableComboBox(QtWidgets.QComboBox):

          def _init_(self, parent: QtCore.QObject) -> None:
              super()._init_(parent=parent)
              self._checkedItems: List[bool] = []
              self.view().pressed.connect(self.handleItemPressed)
              self.setModel(QtGui.QStandardItemModel(self))
              self.model().itemChanged.connect(self.onItemChanged)

          @QtCore.Slot(QtGui.QStandardItem)
          def onItemChanged(self, item: QtGui.QStandardItem) -> None:
              if item.row() < len(self._checkedItems):
                  currentState = self._checkedItems[item.row()]
                  isChecked = item.checkState() == QtCore.Qt.Checked
                  if currentState != isChecked:
                      self._checkedItems[item.row()] = isChecked
       
          @QtCore.Slot(QtCore.QModelIndex)

          def handleItemPressed(self, item: QtCore.QModelIndex) -> None:
              self.setCurrentIndex(item.row())
              self.hidePopup()

          def addItem(self, *args) -> None:
              super().addItem(*args)
              item = self.model().item(self.count() - 1, 0)
              item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
              item.setCheckState(QtCore.Qt.Checked)
              self._checkedItems.append(True)

          def removeItem(self, index: int) -> None:
              del self._checkedItems[index]
              super().removeItem(index)
       

       
       
       
      Upon removing and adding items to the custom combobox, you eventually run into an error message in event filters. There seems to be no root cause on the number of times, but usually within a minute (and certainly within 10minutes) of adding and removing items to the combobox. An error occurs that seems to suggest a possible issue in the memory allocation. The lines that cause the issue are highlighted in bold above and is to do with setting values on the QStandardItem. The error that is produced is below:

      Example error message

      2021-02-18, 11:19:50 TypeError, 'PySide2.QtWidgets.QApplication.notify' called with wrong argument types: PySide2.QtWidgets.QApplication.notify(QMenu, QStandardItem) Supported signatures: PySide2.QtWidgets.QApplication.notify(PySide2.QtCore.QObject, PySide2.QtCore.QEvent)

      {{}}
      An interesting point to note, if in the event filter you simply change the class to QEvent, it carries on correctly and does not complain. Example code below:

      Example code to silence TypeError

      class Application(QtWidgets.QApplication):
          ...
       
          def notify(self, receiver, event):
              if not isinstance(event, QtCore.QEvent):
                  logger.warning("Event has the wrong class type.")
                  event._class_ = QtCore.QEvent
              return QtWidgets.QApplication.notify(self, receiver, event)

       Other solutions for creating a custom QComboBox that does not rely on setting QStandardItem values work (for example, by creating a custom QStandardItemModel that creates the same functionality).

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            crmaurei Cristian Maureira-Fredes
            Reporter:
            lewiscarneyrefeyn Lewis Carney
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes