Please nominate your Qt Champions for 2021!

SQL table view - removed element stays on the view

  • Hello everyone!

    I am having a problem with the presentation of the data with QSql. I am using the default QSQLITE driver.

    I created the button to remove the element in the TableView and everything was working fine, till I used the line:


    After removing it, so that the changes to the database are actually saved to the database file on disk I am having such an effect after removing the element:


    Here the first element should have been removed. The function removeRow is returning true. After the restart of the application the object is gone (as it should):

    The code for the remove slot. As said earlier the function 'removeRow' returns true:

    void DialogOpenArticle::remove()
        auto selected_index_list = ui->tableView->selectionModel()->selectedIndexes();
            QMessageBox::information(0, tr("Error"), tr("No elements selected. Please retry."));
        auto index =;

    And the constructor of the dialog:

    DialogOpenArticle::DialogOpenArticle(QSqlRelationalTableModel *model, QWidget *parent) :
        ui(new Ui::DialogOpenArticle),
       /* ... setup of the irrelevant part of the program ... */

    When I am inserting a new element with 'insertRecord' the change is immediately correctly reflected in the table view. Am I missing some update function? Maybe I could try with a different driver for database, but which one is included also by default so that I can avoid the hassle with the building of the driver just for the test? That would be nice :)

  • Moderators

    So this happens with deletes until you call submitAll(). What you can do is hide the row after the delete or just call submitAll and the model will update properly.

    This is a feature of sql models only and doesn't happen with other memory based models. So of course you could put your sql model into a memory mapped one and that should work as well. That's more work than it's worth though, easiest just to hide the deleted rows until things are committed. Or force the commit with submitAll().

  • Thank you for clearing that up! I was a bit concerned because I have never seen in any example that someone was doing any submit or row hiding. I will check it in the evening and mark the topic as solved.

    EDIT: submitAll did not help in this case, but the usual hiding of the row solved the issue :) The only thing which may be a drawback of this solution is that the numbering of the rows in the table view stays as it was before hiding, so that some numbers are missing. For me luckily it's not a problem.

Log in to reply