Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    sql_model_->setEditStrategy(QSqlTableModel::OnManualSubmit);
    

    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:

    0_1524002536212_Capture.PNG

    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):
    0_1524002656996_Capture2.PNG

    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();
        if(selected_index_list.empty())
        {
            QMessageBox::information(0, tr("Error"), tr("No elements selected. Please retry."));
            return;
        }
        auto index = selected_index_list.at(0);
        model_->removeRow(index.row());
    }
    

    And the constructor of the dialog:

    DialogOpenArticle::DialogOpenArticle(QSqlRelationalTableModel *model, QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DialogOpenArticle),
        model_(model)
    {
       /* ... setup of the irrelevant part of the program ... */
    
        ui->tableView->setModel(model_);
        ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
        ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
    

    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