Results 1 to 2 of 2

Thread: How to check if insert was success in a QTableModel/QTableView approach

  1. #1
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to check if insert was success in a QTableModel/QTableView approach

    Hello,

    This code inserts records in a MySQL table:
    Qt Code:
    1. int row = 0;
    2. model->insertRows(row,1);
    3. QModelIndex index = model->index(row, model->fieldIndex("idPK"));
    4. view->setCurrentIndex(index);
    5. view->edit(index);
    To copy to clipboard, switch view to plain text mode 

    If i repeat the the value in the Primary Key or leave NULL a mandatory field, the data will remain visible in the view but, as soon as i refresh the model, i realise it was not there.

    How showld i control this?

  2. The following 2 users say thank you to aguleo for this useful post:

    talbertypeter (7th December 2013)

  3. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to check if insert was success in a QTableModel/QTableView approach

    For the non-key columns use a delegate to provide an editor that enforces the rules, i.e. Forces a value, certain range etc.

    For the primary key of the table, the answer depends a bit on the nature of the column(s). If the column is a opaque key that autogenerates if a value is not provided on insert to the RDBMS (autoincrement number for example) then do not allow the user to provide a value at all. When the table model is submitted, the column for, which isGenerated() is true, will be omitted from the insert statement and a value created by the RDBMS. You can then access the new record key with lastInsertId().

    If you a have a compound or natural key then use a delegate to try to protect the user from themselves

  4. The following user says thank you to ChrisW67 for this useful post:

    aguleo (6th December 2013)

Similar Threads

  1. Replies: 16
    Last Post: 28th April 2020, 14:40
  2. Qtableview insert row and column
    By maarvi in forum Newbie
    Replies: 1
    Last Post: 1st July 2011, 09:26
  3. Best (and easiest) approach, QCombobox and QTableView...
    By scott_hollen in forum Qt Programming
    Replies: 2
    Last Post: 29th March 2011, 14:07
  4. QTableView QTablemodel & insertRow
    By pfusterschmied in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 12:43
  5. Insert QCheckBox into QTableView
    By wind in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2006, 16:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.