Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QSqlRelationalTableModel::insertRows + sqlite and autoincrementing primary keys
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Apelsinsaft
    wrote on 22 Oct 2013, 16:05 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Apelsinsaft
      wrote on 22 Oct 2013, 20:48 last edited by
      #2

      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.

      1 Reply Last reply
      0

      1/2

      22 Oct 2013, 16:05

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved