Get lastError if failed inserting new row into QSqlTableModel
-
Asked first at StackOverflow, but without answer.
Sometimes, if there is some constraint in my SQL table, inserting new row into QSqlTableView fails. After filling up all fields in the new row (the one with asterisk in the left column), and hitting [Enter] the asterisk doesn't change into a number. This means, my data does not satisfy all constraints. But no error message appears, that points where's the problem (which field must be changed). What signal can I use in order to display the model.lastError() in such a case? I was trying with beginInsertRows(), but it's not suitable for this purpose (see: http://stackoverflow.com/questions/19706707/how-exactly-do-i-use-begininsertrows).I was trying single shot timer, but this will not trigger as the INSERT command doesn't finish in such cases.
Example class:
@ class ViewSQLTable : public View {
Q_OBJECT
protected:
QSqlRelationalTableModel *model;
QTableView view;
QTimer errorChecker;
//...
@Slot used with right_click > New:
@
void ViewSQLTable::New() {
int row = model->rowCount();
if(!model->insertRow(row)) ERR(model->lastError().text());
QModelIndex index = model->index(row, 0);
view->setCurrentIndex(index);
view->edit(index); }
@Single shot timer defined in constructor:
@
errorChecker = new QTimer(this);
errorChecker->setSingleShot(true);
errorChecker->setInterval(20);
//Next line doesn't work...
connect(model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),errorChecker,SLOT(start()));
connect(errorChecker,SIGNAL(timeout()),this,SLOT(CheckErrors()));
//connect(model,SIGNAL(beforeInsert(QSqlRecord&)),QTimer:: -> no idea...
@ -
Hi,
@if(!model->insertRow(row)) ERR(model->lastError().text());@
Why not emit a custom signal here ?
-
Indeed, it only insert a new row, however, you don't call setData anywhere, and AFAIK, it's the function you should check