QSqlTableModel - distinguish between existed and new table rows
-
Hi, all,
I'm a bit puzzled how to work with deleted rows in QSqlTableModel so below is a code example with pictures of how it works and questions.The code: I have very simple sqlite database
d1.sqlite
that contains only 1 tablet1
with 2 fieldsid
andvalue
. It is populated with 3 rows of data:id | value ---|------- 1 | aaa 2 | bbb 3 | ccc
My code displays this table in QTableView and has 2 buttons.
"Add"
button allows to insert one more row into the table and"Remove"
buttons deletes currently selected row.
Important thing - I haveself.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
because in real code I would like to have more control about when I submit or rollback changes.Here you may see how this code works:
- Just after start and selection of 1st row. Table shows 3 rows from the database.
- Button
"Remove"
was pressed and 2nd row was selected. You may see that 1st row is marked with exclamation sign!
- this is how Qt indicates that row was deleted. - Button
"Add"
was pressed. You may see that 4th row was added to the table and this row is marked with asterisk*
- this is how Qt indicates that this is a new row. - New, 4th row, was selected.
- Button
"Remove"
was pressed again. 4th row was correctly deleted and you may see that it disappeared from the table completely. It was not marked with!
as it happened with 1st row but was completely removed.
Actually everything works right and logical, but... I've subclassed
QSqlTableModel
and overriden methodremoveRow()
. My method keeps track of what was deleted from the table and I need to understand was row actually removed from the table by Qt or only marked for deletion (i.e. I need to know is the row still visible for the user or disappeared).
The questions is - how to distinguish between these 2 cases?
I remember that there was no way to check if row is marked for deletion inQSqlTableModel
(I don't think that it have changed recently). So the question is actually - can I see somehow that row is a fresh one, not from the database? (without making explicit SQL query that isn't really good from resource consumption point of view...)Here is the code:
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTableView, QPushButton, QAbstractItemView from PySide6.QtSql import QSqlDatabase, QSqlTableModel class DbWnd(QMainWindow): def __init__(self): super().__init__() self.centralwidget = QWidget() self.layout = QVBoxLayout() self.table = QTableView(self) self.table.setSelectionMode(QAbstractItemView.SingleSelection) self.table.setSelectionBehavior(QAbstractItemView.SelectRows) self.layout.addWidget(self.table) self.add_button = QPushButton(self, text="Add") self.layout.addWidget(self.add_button) self.remove_button = QPushButton(self, text="Remove") self.layout.addWidget(self.remove_button) self.centralwidget.setLayout(self.layout) self.setCentralWidget(self.centralwidget) self.db = QSqlDatabase.addDatabase("QSQLITE", "D1") self.db.setDatabaseName("d1.sqlite") self.db.open() self.model = QSqlTableModel(parent=self, db=self.db) self.model.setEditStrategy(QSqlTableModel.OnManualSubmit) self.model.setTable("t1") self.table.setModel(self.model) self.model.select() self.add_button.clicked.connect(self.add_row) self.remove_button.clicked.connect(self.remove_row) def add_row(self): new_record = self.model.record() new_record.setValue("value", "xxx") self.model.insertRecord(-1, new_record) def remove_row(self): selection = self.table.selectionModel().selection().indexes() for idx in selection: self.model.removeRow(idx.row()) def main(): app = QApplication() wnd = DbWnd() wnd.show() return app.exec()
-
Ok, I was not able to find a generic solution.
But I recognized thatid
is a key field in my tables and normally it shouldn't have0
value. It means that for may particular case I can distinguish rows based on this field value - if it has non-zero value then row came from database otherwise it is a fresh row that was created but haven't been committed to the database yet.Do my question transforms - is there a way to ask for a new feature in Qt?
There is https://bugreports.qt.io/ for Qt bugs but I'm not sure is it allowed to raise feature requests there.
It is obvious that Qt tracks record status internally and it would be good to have it available as a read-only property of table-object. So it shouldn't require much efforts from developers but would be really useful and I would like to make such request but don't know how to do it in a right way. -
Hi,
The ticketing system is used for both bugs and feature requests.
As for how the internal state is managed, you can directly check the implementation of the model classes to see how it is done.
-
@SGaist , ok thanks.
So I created QTBUG-115145 with this proposal, lets see.
This topic is solved. -