Qt6 topics merged with General

How to remove row from QsqlTableModel?

  • I have the following code:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
        m_ui(new Ui::MainWindow),
        m_model(new QSqlTableModel(this))
        qDebug() << "Select       : Row count:" << m_model->rowCount();
        connect(m_ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
                this, &MainWindow::on_selectionChanged);
        delete m_ui;
    void MainWindow::on_pushButtonNewRecord_clicked()
        qDebug() << "New Record";
        m_record = m_model->record();
        m_record.setValue("firstname", "john");
        m_record.setValue("lastname", "doe");
        m_record.setValue("email", "john.doe@email.com");
        m_model->insertRecord(-1, m_record);
        qDebug() << "New Record   : Row count:" << m_model->rowCount();
    void MainWindow::on_pushButtonRemoveRow_clicked()
        qDebug() << "Remove Row";
        if (m_row >= 0) {
            m_model->removeRows(m_row, 1);
            qDebug() << "Remove Record: Row count:" << m_model->rowCount();
    void MainWindow::on_pushButtonSubmit_clicked()
        qDebug() << "Submit";
    void MainWindow::on_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
        if (!selected.isEmpty()) {
            m_row = selected.indexes().first().row();

    I can add new records with insertRecord and remove records with removeRows without any problem, but when I read the records from the database with select and want to remove one of these records, this doesn't work. Even if I call removeRows on them, they are not removed from the model. They will be removed from the model when I call submitAll. How can I achieve that also the selected rows are removed from the model when I call removeRows, without calling submitAll?

  • Lifetime Qt Champion

    I would check for the return value of removeRow() and QSqlQueryModel::lastError()

  • Lifetime Qt Champion


    You have specified the edit strategy to be on manual submit. If you want to have things done automatically then change the edit strategy.

  • @SGaist I don't want to have them automatically done. I would like that the user can add and remove records and that they are written to the database only when submit all is called. The user should be able to add or remove records, but if he is not happy he should be able to cancel the action without writing the values to the database. Is there a way to achieve this?

    Records which are inserted with insertRecord will be removed from the model if removeRows is called. But records which were read from the database will not be removed from the model, when removeRows is called. Is there a way that these records are also remove from the model when removeRows is called or at least they should not be showed in the tableView. When submitAll is called the new records should be written to the database and the records which were removed should be deleted in the database.

    Is there a way to find out which rows of the model were removed but are still in the model? Something like a flag? And that only the rows which are not delete are shown in the tableView.

  • Lifetime Qt Champion

    @Infinity said in How to remove row from QsqlTableModel?:

    Is there a way to find out which rows of the model were removed but are still in the model?

    Did you do what I wrote?

  • @Christian-Ehrlicher Yes. It returns always true.

  • Lifetime Qt Champion

    So when lastError() did not return anything I would expect it was removed from the table. Please provide a minimal, compilable example.

  • If I call removeRows on records which were read from the database a ! will be displayed in the first column of the tableView.


    The only thing which I would like to achieve is that this row will not be displayed in the view. I tried it with QSortFilterProxyModel but I don't know where I can get the flag which is used to display the ! in the first column. Is there a way to set a filter in QSortFilterProxyModel that it only contains the rows which don't have this flag?

    From where does the view take the information, that the removed row is marked with a "!" ? This information might be hidden somewhere in the model, but I cannot find out where.

  • @Infinity
    have you tried out:

    //remember that your edit strategy is OnManualSubmit

    in your removeRow slot ?

  • @Lemat Yes. Thank you very much. I tried that, but that is not what I'm looking for.

Log in to reply