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...
    @


  • Lifetime Qt Champion

    Hi,

    @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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.