I use this code and it works fine:
....
self.query_model = QSqlQueryModel()
view = QTableView()
view.setModel(self.query_model)
... but I would like to use a QAbstractTableModel because then I have more influence on the design of the table. Unfortunately my code doesn't work.
My changed code:
self.query_model = QSqlQueryModel()
tab_model = MyTableModel(self.query_model)
view = QTableView()
view.setModel(tab_model)
and:
class MyTableModel(QAbstractTableModel):
def __init__(self, model):
super().__init__(model)
self._model_sql = model
self._headers = headers
def rowCount(self, index=None):
return self._model_sql.rowCount()
def columnCount(self, index=None):
return self._model_sql.columnCount()
def headerData(self, section, orientation, role):
if role == Qt.ItemDataRole.DisplayRole and orientation == Qt.ItemDataRole.Horizontal:
if section == 0:
return "KDNr."
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
if not index.isValid():
print("index: " + str(index))
return None
elif role == Qt.ItemDataRole.DisplayRole:
column = index.column()
if column == 0:
return self._model_sql.record(index.row()).value(column)
elif column == 1:
value = self._model_sql.record(index.row()).value(column)
return f"RIP {value}"
else:
return None
return None
The table does not appear