[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: ":http://qt-project.org/forums/viewthread/17746":http://qt-project.org/forums/viewthread/17746

    I changed

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

    to

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

    and it works.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.