Editing data in a tableview
-
Hi,
I have been learning QT from a book called "Create GUI applications with Python and QT6" by Martin Fitzpatrick. I have been following along on how to create a tableview using a dataframe as data. and I understand the concepts(basically) of the model principle. My question though is this, I cannot edit the data in the cells. I wish to be able at least be able to modify that, I then want to update the dataframe with the changes. I will list the code below.
Can anyone help me please?import sys
import pandas as pd
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import Qtclass TableModel(QtCore.QAbstractTableModel):
def init(self, data):
super().init()
self._data = datadef data(self, index, role): if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole: 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 headerData(self, section, orientation, role): if role == Qt.ItemDataRole.DisplayRole: if orientation == Qt.Orientation.Horizontal: return str(self._data.columns[section]) if orientation == Qt.Orientation.Vertical: return str(self._data.index[section])
class MainWindow(QtWidgets.QMainWindow):
def init(self):
super().init()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) self.table.setModel(self.model) self.setCentralWidget(self.table) self.setGeometry(600, 100, 400, 200)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec() -
Hi,
I have been learning QT from a book called "Create GUI applications with Python and QT6" by Martin Fitzpatrick. I have been following along on how to create a tableview using a dataframe as data. and I understand the concepts(basically) of the model principle. My question though is this, I cannot edit the data in the cells. I wish to be able at least be able to modify that, I then want to update the dataframe with the changes. I will list the code below.
Can anyone help me please?import sys
import pandas as pd
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import Qtclass TableModel(QtCore.QAbstractTableModel):
def init(self, data):
super().init()
self._data = datadef data(self, index, role): if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole: 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 headerData(self, section, orientation, role): if role == Qt.ItemDataRole.DisplayRole: if orientation == Qt.Orientation.Horizontal: return str(self._data.columns[section]) if orientation == Qt.Orientation.Vertical: return str(self._data.index[section])
class MainWindow(QtWidgets.QMainWindow):
def init(self):
super().init()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) self.table.setModel(self.model) self.setCentralWidget(self.table) self.setGeometry(600, 100, 400, 200)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()@MIckH
First, please enclose blocks of code inside a line of just```
above and below, or use the forum's</>
Code icon when composing your post.You are subclassing
QAbstractTableModel
. So far you have provided the methods required for displaying what is in the model. Now you must add the methods required for editing So read https://doc.qt.io/qtforpython-6/PySide6/QtCore/QAbstractTableModel.html#subclassingEditable models need to implement
setData()
, and implementflags()
to return a value containingItemIsEditable
.and also https://doc.qt.io/qtforpython-6/PySide6/QtCore/QAbstractItemModel.html#subclassing. I think https://www.pythonguis.com/faq/editing-pyqt-tableview/ covers this in first 2 code boxes.
You might also be interested in https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QAbstractItemView.html#PySide6.QtWidgets.PySide6.QtWidgets.QAbstractItemView.setEditTriggers and https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QAbstractItemView.html#PySide6.QtWidgets.PySide6.QtWidgets.QAbstractItemView.editTriggers, which controls what user actions enter editing mode. I think the default is double-click to edit, but you can alter that if you wanted to.