Editing QTableView cells
Allowing users to update the data in a model view

Heads up! You've already completed this tutorial.

Villa wrote

I using QT designer to create my ui file, in QT designer created a QTableView set to edit double click. With Pushbutton I load CSV file into QTableView , I can edit a number but as soon as I press enter number returns to original value without updating the dataframe. In the book create GUI application pyside there is a detailed description using sqlite and editing cell. I prefer to use pandas dataframe in that I need to manipulate data in the dataframe before loading to sqlite or SQL database . Please advise

Martin Fitzpatrick

I'll be expanding the pandas tutorials soon. But generally what you need to do is implement a setData method. This works like the data method, but should accept an index & value and use these to update the dataframe data.

You also need to implement flags to make it editable.

A full working example (for PyQt5) is below, let me know if you're using something else --

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
import pandas as pd

class TableModel(QtCore.QAbstractTableModel):

    def __init__(self, data):
        super(TableModel, self).__init__()
        self._data = data

    def data(self, index, role):
        if role == Qt.DisplayRole:
            value = self._data.iloc[index.row(), index.column()]
            return str(value)

    def rowCount(self, index):
        return self._data.shape[0]

    def columnCount(self, index):
        return self._data.shape[1]

    def flags(self, index):
        if not index.isValid():
            return Qt.ItemIsEnabled

        return super().flags(index) | Qt.ItemIsEditable  # add editable flag.

    def headerData(self, section, orientation, role):
        # section is the index of the column/row.
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return str(self._data.columns[section])

            if orientation == Qt.Vertical:
                return str(self._data.index[section])

    def setData(self, index, value, role):
        if role == Qt.EditRole:
            # Set the value into the frame.
            self._data.iloc[index.row(), index.column()] = value
            return True

        return False

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):

        self.table = QtWidgets.QTableView()

        data = pd.DataFrame([
          [1, 9, 2],
          [1, 0, -1],
          [3, 5, 2],
          [3, 3, 2],
          [5, 8, 9],
        ], columns = ['A', 'B', 'C'], index=['Row 1', 'Row 2', 'Row 3', 'Row 4', 'Row 5'])

        self.model = TableModel(data)



The setData method returns True it it was a success.

Over 10,000 developers have bought Create GUI Applications with Python & Qt!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak