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:

    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 {
    QSqlRelationalTableModel *model;
    QTableView view;

    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->edit(index); }

    Single shot timer defined in constructor:

    errorChecker = new QTimer(this);
    //Next line doesn't work...
    //connect(model,SIGNAL(beforeInsert(QSqlRecord&)),QTimer:: -> no idea...

  • Lifetime Qt Champion


    @if(!model->insertRow(row)) ERR(model->lastError().text());@

    Why not emit a custom signal here ?

  • Thanks for your reply.
    This line does not raise an error message if the data is wrong. -I think insertRow() returns just after inserting new, empty row.
    I need a signal that is emmited after an attempt of inserting new data into the table.

  • Lifetime Qt Champion

    Indeed, it only insert a new row, however, you don't call setData anywhere, and AFAIK, it's the function you should check

Log in to reply