Results 1 to 4 of 4

Thread: howto add new row to QSqlTableModel with QDataWidgetMapper

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question howto add new row to QSqlTableModel with QDataWidgetMapper

    Phase one was creating the form to edit an existing row using QSqlTableModel/QDataWidgetMapper. I figured that out, and in the process, learned a lot. After digging around a bit, I cannot figure out how to go about adding a whole new row.

    As I said in the other post, the final three lines of the constructor are currently:

    Qt Code:
    1. _model->setFilter(QString("PARISHNER_ID = %1").arg(parishionerId));
    2. _model->select();
    3. _mapper->toFirst();
    To copy to clipboard, switch view to plain text mode 

    How do I go about getting the model to a row that does not exist as of yet? so a new parishner can be added?

  2. #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: howto add new row to QSqlTableModel with QDataWidgetMapper

    QAbstractItemModel::insertRow() or QSqlTableModel::insertRecord() and then reposition the QDataWidgetMapper current index.

  3. #3
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: howto add new row to QSqlTableModel with QDataWidgetMapper

    Chris,

    What isn't clicking with me is that I don't have any data to enter yet, so how can I insert something? I don't even have the primary key yet for this new record. If I were to insert an empty record, how do you deal with the user canceling the add?

  4. #4
    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: howto add new row to QSqlTableModel with QDataWidgetMapper

    QSqlTableModel::removeRows() to remove the uncommitted row. Exactly when the data is committed to the database depends on the editStrategy(). If you are on manual submits then this will never go near the database. If you are using the database to generate a unique id for a row then you won't have this until after the data is actually written to the database.

Similar Threads

  1. QDataWidgetmapper
    By locus in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2012, 11:24
  2. Replies: 1
    Last Post: 4th October 2010, 00:46
  3. Replies: 1
    Last Post: 30th July 2010, 07:23
  4. QDataWidgetMapper
    By rogerholmes in forum Newbie
    Replies: 4
    Last Post: 24th March 2009, 19:32
  5. QDataWidgetMapper and QLineEdit
    By GuL in forum Newbie
    Replies: 3
    Last Post: 13th August 2008, 14:58

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.