View Full Version : howto add new row to QSqlTableModel with QDataWidgetMapper

4th October 2010, 01:28
Phase one was creating the form to edit an existing row using QSqlTableModel/QDataWidgetMapper. I figured that out, and in the process, learned a lot. After digging around a bit, I cannot figure out how to go about adding a whole new row.

As I said in the other post, the final three lines of the constructor are currently:

_model->setFilter(QString("PARISHNER_ID = %1").arg(parishionerId));

How do I go about getting the model to a row that does not exist as of yet? so a new parishner can be added?

4th October 2010, 03:44
QAbstractItemModel::insertRow() or QSqlTableModel::insertRecord() and then reposition the QDataWidgetMapper current index.

4th October 2010, 03:49

What isn't clicking with me is that I don't have any data to enter yet, so how can I insert something? I don't even have the primary key yet for this new record. If I were to insert an empty record, how do you deal with the user canceling the add?

4th October 2010, 03:57
QSqlTableModel::removeRows() to remove the uncommitted row. Exactly when the data is committed to the database depends on the editStrategy(). If you are on manual submits then this will never go near the database. If you are using the database to generate a unique id for a row then you won't have this until after the data is actually written to the database.