PDA

View Full Version : How to check if insert was success in a QTableModel/QTableView approach



aguleo
6th December 2013, 15:45
Hello,

This code inserts records in a MySQL table:


int row = 0;
model->insertRows(row,1);
QModelIndex index = model->index(row, model->fieldIndex("idPK"));
view->setCurrentIndex(index);
view->edit(index);


If i repeat the the value in the Primary Key or leave NULL a mandatory field, the data will remain visible in the view but, as soon as i refresh the model, i realise it was not there.

How showld i control this?

ChrisW67
6th December 2013, 20:08
For the non-key columns use a delegate to provide an editor that enforces the rules, i.e. Forces a value, certain range etc.

For the primary key of the table, the answer depends a bit on the nature of the column(s). If the column is a opaque key that autogenerates if a value is not provided on insert to the RDBMS (autoincrement number for example) then do not allow the user to provide a value at all. When the table model is submitted, the column for, which isGenerated() is true, will be omitted from the insert statement and a value created by the RDBMS. You can then access the new record key with lastInsertId().

If you a have a compound or natural key then use a delegate to try to protect the user from themselves