I have subclassed QSQLTableModel
to control different display stuff in Table View (such as red color for negative values, dynamic number of decimal digits, etc..) and that works fine.
When I've tried to remove a row the method is not working, not deleting row in the underlying db.
class CustomSqlTableModel(QSqlTableModel):
def __init__(self, table, *args, **kwargs):
super(CustomSqlTableModel, self).__init__(*args, **kwargs)
self.setTable(table)
self.setEditStrategy(QSqlTableModel.OnManualSubmit) #OnFieldChange
self.select()
def data(self, index, role):
# here several ifs to control display they work fine in TableView
# even if I delete them and just leave out the bellow standard line it will not remove the row
return QSqlTableModel.data(self, index, role)
def setData(self, index, value, role=Qt.EditRole):
if not index.isValid():
return False
return QSqlTableModel.setData(self, index, value, role)
model = CustomSqlTableModel(db=my_conn, table='my_table')
print(model.rowCount()) # returns the number of rows
a = model.removeRow(2)
print(a) # returns True
b = model.submitAll()
print(b) # returns True
c = model.select()
print(c) # returns True
print(model.rowCount()) # returns the same number of rows
If I delete the data
and/or setData
reimplementation of the QSqlTableModel
the removeRow
does work and deletes the row from the database.
But if in the data()
method I just leave the standard line replicating the Base class method the removeRow
will also not work. So it must be something very basic in the subclassing that I am doing wrong.
I've seen in other responses hints that the EditRole and/or Record() method should be considered in the data()
method reimplement but I could not figure that out.