PDA

View Full Version : delete row from QSqlTableModel in QTableView



pippo42
19th January 2010, 11:12
Hello!

I have a QSqlTableModel displayed in a QTableView.

When I try to remove a row, the row does not disappear from the table view. There is just a "!" that appears on the side. I have tried with closePersistentEditor but it does not help.

How is it possible to remove rows from the model?

Thank you!

nikhilqt
19th January 2010, 12:42
qsqltablemodel::removeRows(...) (http://doc.trolltech.com/4.4/qsqltablemodel.html#removeRows) should do the work you require. post some code you tried for.

eldjarn
10th November 2010, 23:22
Hi! I have exactly same problem. Row remains visible in the view until submitAll execution, whitch is rather strange and confusing. At least because when you edit existant item, new value is shown until you ccommit or discard data.

Here is slot, that I use to delete selected rows:


def _removeSelectedStatuses(self):
'''
Удаляет выбранные строки из таблицы

pre[self]: self._model is not None
'''
model = self.ConservationStatusesTableView.selectionModel( )
l = model.selectedRows()
if not len(l): return

rows = set([i.row() for i in l])
rows = list(rows)
rows.sort()
first = rows[0]
count = len(rows)
self._model.removeRows(first, count)

If you know the answer, please, feel free to post it at
http://stackoverflow.com/questions/4146633/row-deleted-from-model-stays-in-view-what-am-i-doing-wrong
too.

ChrisW67
11th November 2010, 00:13
QSqlTableModel caches changes until the submitAll() is called. The way it handles inserts and updates differs from the way it handles deletes. If you have a single model with two views then updates and inserts made in one model are immediately visible in the other view as you would expect. If you remove a row then the row remains in the views with the only thing identifying that the row is scheduled for deletion is the "!" in the header view. This behaviour is unique to the SQL models and does not exist in the in-memory models.

To obtain the behaviour that users expect, i.e. the row disappears when deleted, the views have to either:
Hide the row based on the undocumented "!" marker. This has to happen in every view displaying the data every time a row is removed.
Force a submitAll() and commit all changes after delete. All views on the model then lose their selection and current item and you have to make efforts to maintain these (because that is what users expect). You have also lost the ability to revert pending changes.
Load the table entirely into an in-memory model. Whether this is acceptable is application dependent.


I might have missed something and I'm all ears if someone has the elegant solution.

Al_
18th March 2011, 21:11
To obtain the behaviour that users expect, i.e. the row disappears when deleted, the views have to either:
Hide the row based on the undocumented "!" marker. ...
...

Does anybody know, how to detect the flag for deleted rows in a model? That is the flag corresponding to '!' marker in views. I want to strike-through these rows by returning a strike-through font in model::data(...) when asked for Qt::FontRole.

It is not model->isDirty(index), as this returns true also for changed and not only for deleted cells.

Al_

ChrisW67
19th March 2011, 05:51
Retrieve the vertical header for the row, it is literally "!"

Al_
19th March 2011, 07:48
Thanks, works nicely. :) A bit unusual to look for a string representation as flag, but as long as it works ...

Cheers

Al_

8Observer8
10th September 2014, 20:20
I will keep here very good example (franku's post): http://qt-project.org/forums/viewthread/18581

And this:


void MainWindow::on_actionClear_triggered()
{
int rowCount = m_model->rowCount();
m_model->removeRows( 0, rowCount );
submit();
}

void MainWindow::submit()
{
if( m_model->submitAll() ) {
m_model->database().commit();
} else {
m_model->database().rollback();
}
}