Important: Please read the Qt Code of Conduct -

[SOLVED] QSqlRelationalTableModel::insertRows + sqlite and autoincrementing primary keys

  • Hi,

    I'm using a sqlite database with the Qt sql module.

    My QSqlRelationalTableModel is viewed in a QTableView:

    @ ui->tableView->setModel(tableModel);@

    My table has a primary key with auto increment. In my user interface, I have a button for adding rows to the table.

    I add a row like this:

    @m_tableModel->insertRows(m_tableModel->rowCount(), 1);@

    However, when the new row appears in the view, the primary key is not filled in. I can enter data into the other fields, and sometimes that makes the primary id appear in the primary key field, but not always.

    I'm obviously doing something wrong. I'd appreciate any help, and "best practices" - type information.

    Thanks in advance!

  • Found the answer here: ":":

    I changed

    @m_tableModel->insertRows(m_tableModel->rowCount(), 1);@


    @QSqlRecord newRecord = m_tableModel->record();
    m_tableModel->insertRecord(0, newRecord)@

    and it works.

Log in to reply