Make QTableView editable when model is pandas dataframe

3.9k views Asked by At

In my gui file I create a QTableView as follows (this is the part that is automatically generated by Qt Designer):

self.pnl_results = QtGui.QTableView(self.tab_3)
font = QtGui.QFont()
font.setPointSize(7)
self.pnl_results.setFont(font)
self.pnl_results.setFrameShape(QtGui.QFrame.StyledPanel)
self.pnl_results.setFrameShadow(QtGui.QFrame.Sunken)
self.pnl_results.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers)
self.pnl_results.setShowGrid(True)
self.pnl_results.setSortingEnabled(True)
self.pnl_results.setCornerButtonEnabled(True)
self.pnl_results.setObjectName("pnl_results")

I then define a model that enables me to link the pandas dataframe to the QTableView:

class PandasModel(QtCore.QAbstractTableModel):
    """
    Class to populate a table view with a pandas dataframe
    """

    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:
                return str(self._data.values[index.row()][index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self._data.columns[col]
        return None

    def setData(self, index, value, role):
        if not index.isValid():
            return False
        if role != QtCore.Qt.EditRole:
            return False
        row = index.row()
        if row < 0 or row >= len(self._data.values):
            return False
        column = index.column()
        if column < 0 or column >= self._data.columns.size:
            return False
        self._data.values[row][column] = value
        self.dataChanged.emit(index, index)
        return True

    def flags(self, index):
        flags = super(self.__class__,self).flags(index)
        flags |= QtCore.Qt.ItemIsEditable
        flags |= QtCore.Qt.ItemIsSelectable
        flags |= QtCore.Qt.ItemIsEnabled
        flags |= QtCore.Qt.ItemIsDragEnabled
        flags |= QtCore.Qt.ItemIsDropEnabled
        return flags

and finally a add my pandas dataframe (df) to the model:

model = PandasModel(df)
self.ui.pnl_results.setModel(model)

This correctly displays my pandas dataframe in the QTableView. However, for some reason when I edit the fileds the return to their original values (and also once I edit the field it is starting as empty). How can I make it editable and then write the reults back to the pandas dataframe?

2

There are 2 answers

15
Dmitry On BEST ANSWER

Your model lacks setData method. The default implementation from QtCore.QAbstractTableModel does nothing and returns False. You need to implement this method in your model to make its items editable. If df is the actual container storing the data, you should simply change the value of the item stored in the container in setData method. It could look like this:

def setData(self, index, value, role):
    if not index.isValid():
        return False
    if role != QtCore.Qt.EditRole:
        return False
    row = index.row()
    if row < 0 or row >= len(self._data.values):
        return False
    column = index.column()
    if column < 0 or column >= self._data.columns.size:
        return False
    self._data.values[row][column] = value
    self.dataChanged.emit(index, index)
    return True

You also need to implement flags method to return a value containing QtCore.Qt.ItemIsEditable.

0
tCot On
    def setData(self, index, value, role=Qt.EditRole):
    if index.isValid():
        if role == Qt.EditRole:
            self._data.iat[index.row(),index.column()] = value
            self.dataChanged.emit(index, index)
            return True
    return False

self._data.values[row][column] = value

didn't work for me, but changing with iat worked