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. Understand why a row is not submitted
Forum Updated to NodeBB v4.3 + New Features

Understand why a row is not submitted

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 580 Views 1 Watching
  • 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
    Mark81
    wrote on 27 Nov 2024, 07:23 last edited by
    #1

    I'm using a QSqlTableModel (actually a QSqlRelationalTabelModel) in Qt 6.8.0.
    I set the editStrategy to OnRowChange.
    On the form I show the data using a QTableView.

    It happens that when I insert a new row and press "enter" (or move to another row) the record is not submitted to the database, i.e. the vertical header still show the * sign.

    I guess it is because it cannot be submitted due to a wrong value somewhere. In fact, it does not happens on very simple tables. Right now, I'm not interested to implement a validator (like in this question). I just want to get an error message whenever it cannot save the row.

    I searched in the docs about QSqlTableModel and QTableView but I find nothing about.

    J 1 Reply Last reply 27 Nov 2024, 07:26
    0
    • M Mark81
      27 Nov 2024, 07:23

      I'm using a QSqlTableModel (actually a QSqlRelationalTabelModel) in Qt 6.8.0.
      I set the editStrategy to OnRowChange.
      On the form I show the data using a QTableView.

      It happens that when I insert a new row and press "enter" (or move to another row) the record is not submitted to the database, i.e. the vertical header still show the * sign.

      I guess it is because it cannot be submitted due to a wrong value somewhere. In fact, it does not happens on very simple tables. Right now, I'm not interested to implement a validator (like in this question). I just want to get an error message whenever it cannot save the row.

      I searched in the docs about QSqlTableModel and QTableView but I find nothing about.

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 27 Nov 2024, 07:26 last edited by
      #2

      @Mark81 You can get last error using https://doc.qt.io/qt-6/qsqlquerymodel.html#lastError

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 27 Nov 2024, 07:34
      0
      • J jsulm
        27 Nov 2024, 07:26

        @Mark81 You can get last error using https://doc.qt.io/qt-6/qsqlquerymodel.html#lastError

        M Offline
        M Offline
        Mark81
        wrote on 27 Nov 2024, 07:34 last edited by
        #3

        @jsulm said in Understand why a row is not submitted:

        @Mark81 You can get last error using https://doc.qt.io/qt-6/qsqlquerymodel.html#lastError

        Correct, but when I can check it? It seems it does not emit a signal. So how I can catch the moment to check if it has some errors?

        I tried the following:

        connect(model, &QSqlRelationalTableModel::beforeInsert, this, [=](QSqlRecord &record)
        {
            qDebug() << model->lastError();
        });
        

        actually, the lambda is executed when I try to submit the faulty row pressing enter (like using an already existing id value) but it returns no errors:

        QSqlError("", "No Fields to update", "")

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mark81
          wrote on 27 Nov 2024, 07:44 last edited by
          #4

          Ok, I understood how it works. To show the actual error, in the example:

          UNIQUE constraint failed: table.id Unable to fetch row

          it's not enough to press enter, but I need to click on another row.
          From a user perspective it's not correct: if he presses "enter" it should submit or return an error.
          Of course using the OnManualSubmit option would avoid this scenario but it requires to click on a QPushButton instead of simply press "enter".

          J 1 Reply Last reply 27 Nov 2024, 08:31
          0
          • M Mark81
            27 Nov 2024, 07:44

            Ok, I understood how it works. To show the actual error, in the example:

            UNIQUE constraint failed: table.id Unable to fetch row

            it's not enough to press enter, but I need to click on another row.
            From a user perspective it's not correct: if he presses "enter" it should submit or return an error.
            Of course using the OnManualSubmit option would avoid this scenario but it requires to click on a QPushButton instead of simply press "enter".

            J Offline
            J Offline
            JonB
            wrote on 27 Nov 2024, 08:31 last edited by JonB
            #5

            @Mark81 said in Understand why a row is not submitted:

            it's not enough to press enter, but I need to click on another row.
            From a user perspective it's not correct: if he presses "enter" it should submit or return an error.

            https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum

            QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.

            Of course using the OnManualSubmit option would avoid this scenario but it requires to click on a QPushButton instead of simply press "enter".

            So it sounds like you need to handle pressing Enter on a row/the table yourself. Then you can either select a different row if using OnRowChange or call submitAll() if using OnManualSubmit.

            M 1 Reply Last reply 27 Nov 2024, 08:37
            0
            • J JonB
              27 Nov 2024, 08:31

              @Mark81 said in Understand why a row is not submitted:

              it's not enough to press enter, but I need to click on another row.
              From a user perspective it's not correct: if he presses "enter" it should submit or return an error.

              https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum

              QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.

              Of course using the OnManualSubmit option would avoid this scenario but it requires to click on a QPushButton instead of simply press "enter".

              So it sounds like you need to handle pressing Enter on a row/the table yourself. Then you can either select a different row if using OnRowChange or call submitAll() if using OnManualSubmit.

              M Offline
              M Offline
              Mark81
              wrote on 27 Nov 2024, 08:37 last edited by
              #6

              @JonB said in Understand why a row is not submitted:

              https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum

              QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.

              That's not actually true.
              If you fill a new row correctly and press enter (even with the above edit strategy) the row is submitted immediately without the needs to select a different row. This is not consistent from my point of view.

              J 1 Reply Last reply 27 Nov 2024, 08:54
              0
              • M Mark81
                27 Nov 2024, 08:37

                @JonB said in Understand why a row is not submitted:

                https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum

                QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.

                That's not actually true.
                If you fill a new row correctly and press enter (even with the above edit strategy) the row is submitted immediately without the needs to select a different row. This is not consistent from my point of view.

                J Offline
                J Offline
                JonB
                wrote on 27 Nov 2024, 08:54 last edited by
                #7

                @Mark81
                I am guessing that OnRowChange gets activated on QTableView::currentChanged()? Or maybe not. I don't know exactly what happens on Enter or why you find it differs between a new row versus an existing row, if that is what it does. You may need to handle the Enter key directly, e.g. https://pohlondrej.com/qtableview-excel-like-editing//

                1 Reply Last reply
                0

                1/7

                27 Nov 2024, 07:23

                • Login

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