PDA

View Full Version : Cancelling a pending database insert



innerhippy
30th October 2008, 00:20
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!

caduel
30th October 2008, 07:36
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().

wysota
30th October 2008, 07:59
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.

innerhippy
30th October 2008, 08:53
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.