Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Get lastError if failed inserting new row into QSqlTableModel

    General and Desktop
    2
    4
    1448
    Loading More Posts
    • 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
      mstankie last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • M
          mstankie last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • First post
              Last post