I have a MySql table with a primary key that is an auto increment bigint.

If I have a QSqlTableModel that is set to OnManualSubmit how do I add a new row and retrieve that auto increment value?

Qt Code:
  1. MyClass::onAddNewItem()
  2. {
  3. QSqlTableModel *model = new QSqlTableModel(this);
  4. model->setTable("mytable");
  5. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  6.  
  7. //This selects no records which is a lot faster, imho, then selecting without a filter.
  8. //Especially if mytable has a lot of rows.
  9. model->setFilter("myid is null"); //fyi, myid is column 0 in the model.
  10.  
  11. model->select();
  12.  
  13. //now I know I could use QSqlTableModel::insertRecord() ,
  14. //but I would like to use QAbstractItemModel methods only.
  15. //If using QSqlTableModel specific methods is the answer to my problem,
  16. //or even just a better way of doing it then please let me know.
  17.  
  18. QAbstractItemModel *abModel = model; //Yes I know this is unnecessary but it helps me adhere to my point above.
  19.  
  20. int newRow = abModel->rowCount();
  21. abModel->insertRow(newRow);
  22.  
  23. //Column 0 is the auto id, so I don't set it here.
  24. newIndex = abModel->index(newRow, 1);
  25. abModel->setData(newIndex, "myvalue");
  26. }
  27.  
  28. MyClass::onSaveItem()
  29. {
  30. //Calling abModel->submit() does nothing because of the OnManualSubmit strategy.
  31. //So I created a ModelHandler class that let's me call the correct submit method.
  32. //I'm using this ModelHandler to do other things as well and it looks odd in this example, i know.
  33.  
  34. myAbModelHandler->submit(); //The end result of this is the same as calling model->submitAll();
  35.  
  36. //This prints "FALSE".
  37. qDebug() << "New Index validity:" << newIndex.isValid();
  38.  
  39. //And because of that this statement doesn't work...
  40. qDebug() << "My new row's autoincrement value is: " << newIndex.sibling(newIndex.row(), 0).data().toString();
  41. }
  42.  
  43. MyClass::onCancelItem()
  44. {
  45. //same as the submit thing above except for revert.
  46. myAbModelHandler->revert(); //The end result of this is the same as calling model->revertAll();
  47. }
To copy to clipboard, switch view to plain text mode 

When saving the item my newIndex is invalid because submitAll() reselects the data and invalidates all indexes. Also because of my setFilter my model now has no values in it. Even if I wouldn't use the filter I can't assume it will be the last row in this newly selected dataset. I have no way of knowing which was the newly inserted row so that I can retrieve my auto incremented number from column 0.

I guess an option would be to change the EditStrategy on the fly. But if I do that, when I call insertRow() it will automatically add that new row to the database. If I want to provide my user with a way of cancelling the new item I would have to delete the row instead of just calling revert(). This would also increase my auto increment number needlessly.

Any wisdom would be very much appreciated!