Results 1 to 2 of 2

Thread: QDataWidgetMapper::submit() updates but not inserts a record

  1. #1
    Join Date
    Dec 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default QDataWidgetMapper::submit() updates but not inserts a record

    Hi Guys,

    Here's the table:
    Qt Code:
    1. CREATE TABLE IF NOT EXISTS PersonsTbl(
    2. ID INTEGER NOT NULL AUTO_INCREMENT,
    3. NAME VARCHAR(100) NOT NULL,
    4. MOL VARCHAR(100),
    5. BULSTAT_EGN NUMERIC(10) NOT NULL,
    6. IDNUMDDS VARCHAR(11),
    7. CONTACT VARCHAR(100),
    8. FLAG NUMERIC(1) NOT NULL CHECK(FLAG IN (0, 1)),
    9. TYPEID INTEGER NOT NULL,
    10. CONSTRAINT PersonsTbl_PK PRIMARY KEY(ID));
    To copy to clipboard, switch view to plain text mode 

    This is the actual mapping of the fields:
    Qt Code:
    1. ...
    2. orderClientMapper = new QDataWidgetMapper();
    3. orderClientMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    4. orderClientMapper->setModel(clientTableModel);
    5. orderClientMapper->setItemDelegate(new QSqlRelationalDelegate());
    6. orderClientMapper->addMapping(clientTypeComboBox, Person_Flag);
    7. orderClientMapper->addMapping(clientNameEdit, Person_Name);
    8. orderClientMapper->addMapping(clientMOLEdit, Person_MOL);
    9. orderClientMapper->addMapping(clientBulstatEgnEdit, Person_ENG_Bulstat);
    10. orderClientMapper->addMapping(clientIdNumDDSEdit, Person_IDNumDDS);
    11. orderClientMapper->addMapping(clientContactEdit, Person_Contact);
    12. ...
    13. orderClientMapper->toLast();
    To copy to clipboard, switch view to plain text mode 

    When Add button is clicked it should submit a new record but instead it updates the one record I had manually entered
    in the DB, i.e. it does not autoincrement the PK.

    Here's the actual submit:
    Qt Code:
    1. int clientRow = orderClientMapper->currentIndex();
    2. QSqlRecord record= clientTableModel->record(clientRow);
    3. record.setValue(Person_Type, 1);
    4. orderClientMapper->submit();
    5. clientTableModel->insertRow(clientRow);
    To copy to clipboard, switch view to plain text mode 

    If you have any thoughts why this happens please do share.

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

    Default Re: QDataWidgetMapper::submit() updates but not inserts a record

    It is doing exactly what you asked it to. You moved the mapper to the last entry in the mode [2nd listing 13], which it then displays. The user edits the displayed record and submits the changes [3rd listing 4] updating the existing record.

    If you want to insert a new row at the end of the the model then call model->insertRow(model->rowCount()) and move the mapper to the inserted (empty) row (mapper->toLast()), allow the user to edit it, and submit().

    I am not sure what you are trying to achieve at lines 2, 3, and 5 of the third listing. You get a copy of the data from the row the user is seeing, modify a value in the copy and then do nothing with the record. At line 5 you actually insert a blank row in the model.

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

    datanas (16th June 2012)

Similar Threads

  1. How to move QDataWidgetMapper to a specific record ?
    By marcvanriet in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2010, 20:51
  2. Replies: 0
    Last Post: 17th October 2010, 12:53
  3. Replies: 1
    Last Post: 4th October 2010, 00:46
  4. Replies: 3
    Last Post: 21st January 2008, 12:22
  5. QDataTable Inserts and Updates
    By ederbs in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2006, 23:23

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
  •  
Qt is a trademark of The Qt Company.