Results 1 to 3 of 3

Thread: QSqlTableModel Master/Detail example problem

  1. #1
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlTableModel Master/Detail example problem

    running this code

    Qt Code:
    1. MasterDialog dlg(this->master, this->details, index, this);
    2. if(dlg.exec() == QDialog::Accepted){
    3.  
    4. dlg.submit();
    5.  
    6. if(!this->master->submitAll()){
    7. QMessageBox::critical(this, "cant save data", master->lastError().text());
    8. this->master->revertAll();
    9. this->details->revertAll();
    10. return;
    11. }
    12.  
    13. if(!this->details->submitAll()){
    14. QMessageBox::critical(this, "cant save data", details->lastError().text());
    15. this->master->revertAll();
    16. this->details->revertAll();
    17. return;
    18. }
    19.  
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    changes from the details model aren't saved to the database. If you comment

    Qt Code:
    1. if(!this->master->submitAll()){
    2. QMessageBox::critical(this, "cant save data", master->lastError().text());
    3. this->master->revertAll();
    4. this->details->revertAll();
    5. return;
    6. }
    To copy to clipboard, switch view to plain text mode 

    the details are correctly saved.
    the project is attached. It uses a sqlite memory db so it should be simple to run and test it.
    Attached Files Attached Files
    --
    MementoMori

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSqlTableModel Master/Detail example problem

    Try calling details->submittAll() first.

  3. #3
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel Master/Detail example problem

    Quote Originally Posted by norobro View Post
    Try calling details->submittAll() first.
    Even if it worked in this case it's an error because if details contains fields (foreign key values) referring to master records which aren't in the database you'll get a foreign key violation error. This will surely happen every time you add a new master and its details at the same time.
    so you always have to commit master changes before committing details ones.
    --
    MementoMori

Similar Threads

  1. Detail Information
    By KaptainKarl in forum Newbie
    Replies: 1
    Last Post: 8th November 2010, 22:39
  2. Master/detail presentation
    By cia.michele in forum Qt Programming
    Replies: 8
    Last Post: 18th August 2010, 14:42
  3. Hide and Show problem in master layout
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2009, 17:04
  4. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 15:31
  5. Replies: 6
    Last Post: 27th February 2006, 13:47

Tags for this Thread

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.