Results 1 to 4 of 4

Thread: How to append the next Record into QTableView without removing the previous record?

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to append the next Record into QTableView without removing the previous record?

    Hi All,

    I have a problem when I'm coding. I aim to add/append another record to my QTableView without removing previous record showed. The code below used to be like that. Have you any solution for me?

    Thanks a lot.

    Qt Code:
    1. model->setTable("Users");
    2. model->setSort(User_Id, Qt::AscendingOrder);
    3. model->setHeaderData(User_Name,Qt::Horizontal,tr("Username"));
    4. model->setHeaderData(User_FirstName, Qt::Horizontal, tr("First name"));
    5. model->setHeaderData(User_LastName, Qt::Horizontal, tr("Last Name"));
    6. model->setRelation(User_Mode,QSqlRelation("UserModes","ID","Name"));
    7. model->setHeaderData(User_Mode, Qt::Horizontal, tr("Type"));
    8. model->select();
    To copy to clipboard, switch view to plain text mode 

  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: How to append the next Record into QTableView without removing the previous reco

    Nothing in the code you have posted even hints that you have tried to add a record. Have you tried anything that failed? Have you read the documentation? Looked at the Qt bundled examples?

    Start with the QSqlTableModel and QAbstractItemModel class pages and the Model/View Programming Overview in the Qt reference documentation

  3. #3
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to append the next Record into QTableView without removing the previous reco

    Yes I've looked it. But can't still work.

    It seems like this:
    Qt Code:
    1. QSqlRecord record = modelTableAction->record(0);
    2. record.setValue(Action_Panel,QVariant(tr(panel)));
    3. record.setValue(Action_SwitchName,QVariant(tr(switchName)));
    4. record.setValue(Action_StateName,QVariant(tr(stateName)));
    5.  
    6. modelTableAction->setRecord(i,record);
    7.  
    8. modelTableAction->insertRecord(0,record);
    9. modelTableAction->submitAll();
    To copy to clipboard, switch view to plain text mode 

  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: How to append the next Record into QTableView without removing the previous reco

    Ah, so the problem is that the current index/selection of the views(s) is lost.

    The moment you call submitAll() on the QSqlTableModel all the attached views lose their selection and current index (in the docs). Neither QModelIndex nor QPersistentModelIndex is of any use to identify a row through the submitAll().

    In the OnManualSubmit edit strategy, if you insert the row using QAbstractItemModel::insertRows() and then set the fields using setData() without calling submitAll() the selection is not affected. If you only add or edit records then this is the approach for you. Deleting records is problematic, because the "deleted" row still exists for this and any other view using it (its headerData() becomes "!").

    Another approach is to:
    • Get the currentIndex() before the submitAll()
    • Fetch a unique identifier from the record containing that index (preferably in a single column)
    • Execute the QSqlTableModel::submitAll()
    • Use QAbstractItemModel::match() to find your unique key
    • Restore the selection using the index returned by match()

    This approach fails if:
    • The records do not have a unique identifier
    • The current row is new and the identifier is automatically allocated by the RDBMS when the row is written (that doesn't happen until the submitAll() is called)

    I also haven't found the right place for other views to catch this event and preserve their own selections.

    For the OnFieldChange and OnRowChange edit strategies the submit comes immediately and selections are lost. You can capture the selection in slots connected to the model's beforeInsert(), beforeUpdate() and beforeDelete() signals but I have yet to find somewhere I can restore an adjusted selection.

    I'm all ears for an elegant solution.

Similar Threads

  1. Replies: 0
    Last Post: 11th March 2010, 08:41
  2. Selected Record...
    By Nefastious in forum Newbie
    Replies: 1
    Last Post: 27th October 2009, 10:54
  3. Inserting Record
    By Nefastious in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 05:28
  4. QTableView, add new record?
    By grellsworth in forum Qt Programming
    Replies: 6
    Last Post: 5th July 2007, 16:08
  5. double record insertion
    By tranfuga25s in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2006, 18:12

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.