Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get lastError if failed inserting new row into QSqlTableModel

Get lastError if failed inserting new row into QSqlTableModel

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mstankie
    wrote on last edited by
    #1

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

    MaciekS

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Why not emit a custom signal here ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mstankie
        wrote on last edited by
        #3

        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.

        MaciekS

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved