Results 1 to 7 of 7

Thread: setData fails in QSqlRelationalTableModel

  1. #1
    Join Date
    Jul 2009
    Posts
    10
    Thanked 1 Time in 1 Post

    Default setData fails in QSqlRelationalTableModel

    I have a QSqlRelationalTableModel that references a simple lookup table containing a primary key named id of type text (string uuid) and a name field of type text. When I insert into the QSqlRelationalTableModel, it only uses the most recently created id in the lookup table.

    When I insert a new item in the lookup table, and then attempt to add a new item in the QSqlRelationalTableModel, the new item regularly fails on setData on the column with the relationship. It doesn't always fail, but it usually does. If it fails using that key, it will always fail with that key. If it doesn't, it will always succeed with that key.

    Qt Code:
    1. if( !_itemModel->setData(_itemModel->index(0,4), _locationUuid) )
    2. {
    3. QMessageBox::critical(0, "set data 4 failed",
    4. _itemModel->lastError().text(), QMessageBox::Cancel);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I can't figure out what the cause is. The _itemModel->lastError() doesn't return any text. If I look at the table with a third party tool, the item was properly inserted into the lookup table. Am I supposed to do something to the model to tell it that the data it references was updated?

    Also, the lookup table is being managed by a QSqlTableModel. The insert looks like this:

    Qt Code:
    1. if( !_locationModel->insertRow(0) )
    2. {
    3. QMessageBox::critical(0, "insert failed",
    4. _locationModel->lastError().text(), QMessageBox::Cancel);
    5. }
    6.  
    7. if( !_locationModel->setData(_locationModel->index(0,0), _locationUuid) )
    8. {
    9. QMessageBox::critical(0, "set data 1 failed",
    10. _locationModel->lastError().text(), QMessageBox::Cancel);
    11. }
    12.  
    13. if( !_locationModel->setData(_locationModel->index(0,1),location) )
    14. {
    15. QMessageBox::critical(0, "set data 2 failed",
    16. _locationModel->lastError().text(), QMessageBox::Cancel);
    17. }
    18.  
    19. if( !_locationModel->submitAll() )
    20. {
    21. QMessageBox::critical(0, "submit failed",
    22. _locationModel->lastError().text(), QMessageBox::Cancel);
    23. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setData fails in QSqlRelationalTableModel

    Did you reset the model after adding the data to one of the tables in the database? There is no notification mechanism running so if you add some data to a database without letting the model know what you did, it will not detect the changes by itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Posts
    10
    Thanked 1 Time in 1 Post

    Default Re: setData fails in QSqlRelationalTableModel

    I tried calling select on _itemModel (the QSqlRelationalTableModel) after adding an item to the lookup table. That didn't seem to help at the time. Is there something else I should be looking at?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setData fails in QSqlRelationalTableModel

    As far as I remember calling select() again is a no-op. Try calling clear() and then select(). Better yet, add records to the lookup table through the relation model (QSqlRelationalModel::relationModel()) instead of manual calls, then you shouldn't need to synchronize anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2009
    Posts
    10
    Thanked 1 Time in 1 Post

    Default Re: setData fails in QSqlRelationalTableModel

    I think that last one will work. I didn't realize you could get the internal QSqlTableModel of the related table. I'll try it tonight.

    This does bring up an interesting question. How should you handle a scenario where there are two QSqlRelationalTableModels referring to the same lookup table? It appears that they would each have their own QSqlTableModel wrapping the lookup table, in which case one would always have to be manually updated, or is that somehow shared between them. It doesn't appear you can manually set the QSqlTableModel that manages the represented table.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setData fails in QSqlRelationalTableModel

    Yes, seems so. You could work around it by working with QSqlTableModel and provide a custom "intelligent" delegate that would do lookups for both models using its own internal (single) model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2009
    Posts
    10
    Thanked 1 Time in 1 Post

    Default Re: setData fails in QSqlRelationalTableModel

    I finally got it to work. I used the table model returned by relationModel to insert into the lookup table. I also had to call submitAll on the QSqlRelationalTableModel itself (after calling it on the relation tableModel) to get that table to notice that lookup table had changed. I don't know if calling submitAll on the relation tableModel is actually rquired, but it had to be talled on the QSqlRelationalTableModel.

Similar Threads

  1. qobject_cast fails across dynamic library boundaries
    By JPNaude in forum Qt Programming
    Replies: 9
    Last Post: 30th December 2008, 17:36
  2. QProcess::startDetached fails on Mac
    By sgmurphy19 in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2008, 09:26
  3. TableModel update fails with qt4.4.1
    By maxel in forum Qt Programming
    Replies: 0
    Last Post: 4th September 2008, 18:01
  4. Release build fails to find some resource images
    By MrGarbage in forum Installation and Deployment
    Replies: 3
    Last Post: 8th December 2007, 16:04
  5. QtService fails to start
    By a550ee in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2006, 13:34

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.