Results 1 to 4 of 4

Thread: Cancelling a pending database insert

  1. #1
    Join Date
    Oct 2008
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Cancelling a pending database insert

    Hello smart people.

    Got an interesting little problem here. I'm using QSqlTableModel (subclassed) and need to validate new records before they are committed. So, I use the beforeInsert signal and inspect the record - if it fails my validation then I need to cancel the insert.

    I would have though that calling revertAll() would accomplish this, but it has an unfortunate side effect of core dumping. Hmmmm.

    Sorry, no useful back trace - top of stack is QSqlTableModel::submitAll()

    I'm using setEditStrategy(QSqlTableModel::OnManualSubmit) and using MySql 5.0

    Any idea how to cancel inserts using this signal? I'd prefer not to trawl through the model's records and remove() on the last one - seems a bit ugly.

    Hope you can help!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Cancelling a pending database insert

    How about this way? Overwrite the "almost virtual" (it's a slot) submitAll() method and check the records for validity. If they are not ok, return false. Otherwise call the base class's submitAll().

  3. #3
    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: Cancelling a pending database insert

    Quote Originally Posted by innerhippy View Post
    Got an interesting little problem here. I'm using QSqlTableModel (subclassed) and need to validate new records before they are committed. So, I use the beforeInsert signal and inspect the record - if it fails my validation then I need to cancel the insert.
    Why not do the validation before the data reaches the model at all? If that's not what you want then you can change the record within the signal you mentioned to an invalid (i.e. empty) record and the insertion will then probably fail resulting in what you wanted.

  4. #4
    Join Date
    Oct 2008
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Cancelling a pending database insert

    Thanks for the replies.
    The commit operation is triggered manually by the user when all the fields (ie columns) have been set. The user has a bunch of ammended records that will only be witten upon explicit instruction, which is when I need the validation to happen.

    I have tried setting the record to empty in the signal but it seems a bit of a hack.

    I was wondering if I was using the revertAll method correctly, and if so if this is a bug or not.

    I'll overrite the submitAll slot instead and see how this goes.

    ps I only started with this Qt stuff last week and I have to say it rocks! And that includes this forum, so thanks to all contributors.

Similar Threads

  1. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  2. how to insert an ' in a database
    By jh in forum General Programming
    Replies: 3
    Last Post: 17th August 2006, 02:47
  3. Replies: 7
    Last Post: 12th August 2006, 15:11

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.