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

Strange things happening to QSqlTableModel (while inserting, deleting, updating)



  • The code:

    model = new QSqlTableModel(this, db1);
    model->setTable("subjects");
    model->setEditStrategy(QSqlTableModel::OnFieldChange);
    model->select();
    model->removeColumn(0); // don't show the ID
    ui->tableView->setModel(model);
    

    Inserting:

    model->insertRow(0);
    

    Deleting:

    QModelIndexList selection = ui->tableView->selectionModel()->selectedRows();
    if (selection.count())
    {
        for (int i = 0; i < selection.count(); i++)
        {
            QModelIndex index = selection.at(i);
            qDebug() << index.row();
            qDebug() << model->removeRow(selection.at(i).row());
            model->select();
            qDebug() << model->lastError();
           //model->submitAll();
        }
        //model1->submitAll(); 
    }
    

    So, the table displays correctly.
    When I edit a cell, the row gets empty and shows !, like it was deleted. Nothing changes in DB.
    When I delete a row with algorithm above, removeRow returns true, the table view blinks like it's re-rendering, but nothing changes.
    When I insert a row, edit it, it gets empty and shows !, BUT it appears in DB and it's in the table when I reopen the app.
    When I un-comment model->submitAll(); nothing changes.
    Any ideas?


Log in to reply