How to insert a new record into QSqlTableModel

  • Hello all,
    I have QSqlTableModel and connected QTableView. Table that is set in model has column ID with autoincrement flag.
    I want to insert a new record into the table and show a new row in QTableView with correctly filled ID.

    I tried
    int row=model.rowCount();
    but I do not know how to get and show correct ID value.

    Thanks in advance,

  • Getting the right ID is impossible until you actually add the row in the database -- you COULD ask the database for the next value, but nothing prevents other users to perform inserts in the meanwhile, thus incrementing the counter. I'd say it's better for you to show a dummy value for that column (possibly greyed out or something), and show the right ID only after you performed the insertion.

Log in to reply