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

Qt.Checked state isn't rendered correctly by Qt.CheckStateRole in QTableView

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P1: Critical
    • None
    • 6.2.4
    • PySide
    • None
    • Python 3.10.5
      OS: macOS 12.4
      PySide: 6.2.4 (installed via pip)
    • macOS

    Description

      Checkbox placed in QTableView by setting Qt.ItemIsUserCheckable flag in the QAbstractTableModel subclass just doesn't check event the CheckStateRole data is successfully set to Qt.Checked.
      my model, which worked perfectly with PySide 6.2.3:

      class pandasModel(QtCore.QAbstractTableModel):
          def __init__(self, data:pd.DataFrame):
              super(pandasModel, self).__init__()
              self._data = data
      
          def rowCount(self, index):
              return self._data.shape[0]
          
          def columnCount(self, index):
              return self._data.shape[1]
      
          def data(self, index, role):
              row = index.row()
              column = index.column()
              value = self._data.iloc[row, column]
              if role == QtCore.Qt.DisplayRole:
                  if value == True:
                      return "Yes"
                  elif value == False:
                      return "No"
                  else:
                      return str(value)
              if role == QtCore.Qt.CheckStateRole and column == 2:
                  if value == True:
                      return QtCore.Qt.Checked
                  else:
                      return QtCore.Qt.Unchecked
      
          def setData(self, index, value, role):
              row = index.row()
              column = index.column()
              if role == QtCore.Qt.CheckStateRole and column == 2:
                  if value == QtCore.Qt.Checked:
                      self._data.iloc[row, column] = True
                      self.dataChanged.emit(index, index)
                  elif value == QtCore.Qt.Unchecked:
                      self._data.iloc[row, column] = False
                      self.dataChanged.emit(index, index)
                  return True
              return False
      
          def flags(self, index):
              flags = super().flags(index)
              if index.column() == 2:
                  flags |= QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsUserCheckable
              return flags
      
          def headerData(self, section, orientation, role):
              # section is the index of the column/row.
              if role == QtCore.Qt.DisplayRole:
                  if orientation == QtCore.Qt.Horizontal:
                      return str(self._data.columns[section])
                  elif orientation == QtCore.Qt.Vertical:
                      return str(self._data.index[section])

      This issue occured after I upgrade PySide6 from 6.2.3 to 6.2.4, while my code worked perfectly with 6.2.3.

      referrence topic in QtForum

       

      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
              wilsli Wilson Li
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes