im try to create small app in PyQT that show table from database and user will be able to add new rows and fill them. Table's data taken from pd.dataframe with 2 cols and dates-indexes. New inserted rows must be with new index == last date-index + 1 day and 2 empty editable items App looks like this
And i want it to be like this
So, I spent many days on how to do this, but did not understand. This question may seem Newbee, but if someone can help I will be grateful
here is a full reproducible code
import sys
import pandas as pd
from PyQt5.QtCore import QAbstractTableModel, QModelIndex, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QWidget, QGridLayout, QPushButton
class TableModel(QAbstractTableModel):
def __init__(self, table_data, parent=None):
super().__init__(parent)
self.table_data = table_data
def rowCount(self, parent=None) -> int:
return self.table_data.shape[0]
def columnCount(self, parent=None) -> int:
return self.table_data.shape[1]
def data(self, index: QModelIndex, role: int = ...):
if role == Qt.DisplayRole:
row = self.table_data.index[index.row()]
col = self.table_data.columns[index.column()]
value = self.table_data.loc[row][col]
return str(value)
def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return str(self.table_data.columns[section])
if orientation == Qt.Vertical:
return str(self.table_data.index[section])
def insertRows(self, row: int, count: int, parent: QModelIndex = ...) -> bool:
self.beginInsertRows()
# whats here?
self.endInsertRows()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QMainWindow()
widget = QWidget()
layout = QGridLayout()
widget.setLayout(layout)
indexes = ['2023-01-03', '2023-01-02', '2023-01-01']
data = {'col1': ['aa', 'bb', 'cc'], 'col2': [44, 55, 66]}
table_data = pd.DataFrame(data=data, index=indexes)
model = TableModel(table_data=table_data)
table = QTableView()
table.setModel(model)
layout.addWidget(table)
button = QPushButton("add row")
button.clicked.connect(model.insertRows)
button_2 = QPushButton("save changes")
layout.addWidget(button)
layout.addWidget(button_2)
win.setCentralWidget(widget)
win.show()
app.exec()