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. How to insert a new record into QSqlTableModel

How to insert a new record into QSqlTableModel

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.0k 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.
  • J Offline
    J Offline
    jarda
    wrote on last edited by
    #1

    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();
    model.insertRow(row);
    but I do not know how to get and show correct ID value.

    Thanks in advance,
    Jarda

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      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.

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0

      • Login

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