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

Inconsistent behaviour between enums returned from QAbstractItemModel.data() and QModelIndex.data()

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • Not Evaluated
    • None
    • 6.5.0, 6.4.3
    • PySide
    • None

    Description

       

      Consider the following example:

      import enum
      from typing import Any, Optional, Union
      from PySide6 import QtCore
      
      class MyEnum(enum.Enum):
          CAR = 0
          HORSE = 1
          BIKE = 2
      
      class VehiclesAndDaysModel(QtCore.QAbstractTableModel):
          def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
              super().__init__(parent=parent)
              self._dataModel = [
                  MyEnum.BIKE,
                  MyEnum.CAR,
                  MyEnum.CAR,
                  MyEnum.BIKE,
                  MyEnum.HORSE,
              ]
          def columnCount(
              self,
              parent: Union[
                  QtCore.QModelIndex, QtCore.QPersistentModelIndex
              ] = QtCore.QModelIndex(),
          ) -> int:
              return 1
          def rowCount(
              self,
              parent: Union[
                  QtCore.QModelIndex, QtCore.QPersistentModelIndex
              ] = QtCore.QModelIndex(),
          ) -> int:
              return len(self._dataModel)
          def data(
              self,
              index: Union[QtCore.QModelIndex, QtCore.QPersistentModelIndex],
              role: int = QtCore.Qt.ItemDataRole.DisplayRole,
          ) -> Any:
              return self._dataModel[index.row()]
      
      testModel = VehiclesAndDaysModel()
      testIndex = testModel.index(2, 0)
      testData = testModel.data(testIndex)
      print(type(testData))
      print(type(testIndex.data()))
      

      This produces the following output:

       

      <enum 'MyEnum'>
      <class 'int'>

       

      This is a change in behaviour when upgrading form 6.3.2.

      In older versions both statements return the properly typed Enum and I think that's the correct behaviour

       

       

      Attachments

        Issue Links

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

          Activity

            People

              crmaurei Cristian Maureira-Fredes
              viktor-refeyn Viktor Verebelyi
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes