Results 1 to 7 of 7

Thread: QPersistentModelIndex and QDataWidgetMapper

  1. #1
    Join Date
    Jan 2008
    Posts
    39
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QPersistentModelIndex and QDataWidgetMapper

    Hi

    I am using a QDataWidgetMapper with a QSqlRelationalTableModel (Manual edit strategy). When a row is inserted into the model, I want the datawidgetmapper to display that row after it is saved to the database. I understand that when a row is inserted into the model, the model indexes are invalid and so I have been looking at using a QPersistentModelIndex to hold the index of the new row. The problem that I am having is that the persistent index appears to be invalid after model.submitAll.

    sample code:
    Qt Code:
    1. row = self.detailMapper.currentIndex()
    2. persistentIndex = QPersistentModelIndex(self.model.index(row,0))
    3. self.detailMapper.submit()
    4. self.model.submitAll()
    5. self.detailMapper.setCurrentIndex(persistentIndex)
    To copy to clipboard, switch view to plain text mode 

    Is this the right approach and how do I use persistent model indices correctly. I have looked at the documentation and posts on this forum but am not able to solve my problem.

    Thanks in advance
    Last edited by jpn; 1st July 2008 at 08:40. Reason: missing [code] tags

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

    Default Re: QPersistentModelIndex and QDataWidgetMapper

    The persistent index will become invalid after the model is being refreshed (and that's what happens to the sql model after data is submitted to the database). The only thing you can do is to remember the primary key of the item and reselect it after the commit.

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

    Chromatix (12th October 2014)

  4. #3
    Join Date
    Jan 2008
    Posts
    39
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPersistentModelIndex and QDataWidgetMapper

    Thanks for that.
    In what cases then would you use a PersistentModelIndex??

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

    Default Re: QPersistentModelIndex and QDataWidgetMapper

    When you edit a model - in that case if you insert rows inbetween other rows, persistent indexes will make sure they still point to the same rows than before the insert. Unfortunately they become invalid when you reset the model (and that's what happens after you commit changes to the database).

  6. #5
    Join Date
    Jan 2008
    Posts
    39
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPersistentModelIndex and QDataWidgetMapper

    OK thanks.

    I am using a QSqlRelationalTableModel and a QDataWidgetMapper. What is the best way to manage transactions?

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

    Default Re: QPersistentModelIndex and QDataWidgetMapper

    Your model is a subclass of QSqlTableModel that has a QSqlTableModel::database() member which gives you access to the database that in turn has methods called QSqlDatabase::transaction(), QSqlDatabase::rollback() and QSqlDatabase::commit().

  8. #7
    Join Date
    Jan 2008
    Posts
    39
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPersistentModelIndex and QDataWidgetMapper

    Thanks again for that.

Similar Threads

  1. QDataWidgetmapper
    By locus in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2012, 11:24
  2. QCheckBox on QDataWidgetMapper
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2008, 03:09
  3. QDataWidgetMapper and QSqlRelationalTableModel problem
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 15:46
  4. QDataWidgetMapper question
    By larry104 in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2007, 15:30
  5. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50

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.