3

I have a pandas dataframe that I would like to present in a QtableView and make it editable. I have create the below model, but for some reason the output has checkboxes in every field. How can I get rid of them?

The outout looks like this: enter image description here

And this this is the model that is used to make the pandas dataframe shown in a qtavleview and make it editable (I'm using PySide)

class PandasModelEditable(QtCore.QAbstractTableModel):
    def __init__(self, data, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._data = data

    def rowCount(self, parent=None):
        return len(self._data.values)

    def columnCount(self, parent=None):
        return self._data.columns.size

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
                return unicode(self._data.iloc[index.row(), index.column()])
        return unicode()

    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return None
        if orientation == QtCore.Qt.Horizontal:
            try:
                return '%s' % unicode(self._data.columns.tolist()[section])
            except (IndexError,):
                return unicode()
        elif orientation == QtCore.Qt.Vertical:
            try:
                return '%s' % unicode(self._data.index.tolist()[section])
            except (IndexError,):
                return unicode()

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | \
               QtCore.Qt.ItemIsEditable

    def setData(self, index, value, role=QtCore.Qt.EditRole):
        if index.isValid():
            self._data.iloc[index.row(), index.column()] = value
            if self.data(index, QtCore.Qt.DisplayRole) == value:
                self.dataChanged.emit(index, index)
                return True
        return unicode()

Removing QtCore.Qt.ItemIsSelectable does not solve the problem as it doesn't seem to have any effet.

Nickpick
  • 6,163
  • 16
  • 65
  • 116

1 Answers1

5

You are returning the wrong default values from data and setaData. The former should return None (so you could just remove the last line), whilst the latter should return False.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • That solved the problem indeed. Any explanation why this results in such unexpected behaviour? – Nickpick Dec 20 '16 at 09:44
  • 1
    @nickpick. The `data` method returns a `QVariant`, so Qt will just convert it to whatever value is appropriate for the given role. An empty string converts to zero, which is equivalent to `Qt.Unchecked`. – ekhumoro Dec 20 '16 at 17:04
  • I have tried to point to a a problem I have with this code, but this response has been deleted here, so I have opened up a new question: https://stackoverflow.com/questions/44743509/editable-qtableview-and-pandas-does-not-work-properly – tfv Jun 25 '17 at 05:35