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. QSqlTableModel signal on insert row submit

QSqlTableModel signal on insert row submit

Scheduled Pinned Locked Moved Solved General and Desktop
qt5qsqltablemodelqsqlrelational
9 Posts 3 Posters 2.4k 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.
  • K Offline
    K Offline
    Kot Shrodingera
    wrote on 24 Oct 2018, 08:33 last edited by Kot Shrodingera
    #1

    I am subclassing QSqlTableModel (QSqlRelationalModel to be presize), editStrategy is OnFieldChange, dbdriver is QSQLITE. Table structure has some NOT NULL fields. Also I have QTableView and button that calls model->insertRows(model->rowCount()). When it is clicked, new row is inserted in view with asterisk(*) instead of row number (as I understand it, because NOT NULL fields aren't defined), when i define all NOT NULL fields and press Enter, asterisk changing to valid row number, so record is succesfully inserted into database. I want to have signal when this is happening. QAbstractTableModel::rowsInserted is called when empty row is added, so it's not suitable. I tried to catch QSqlTableModel::submit() method and its return value: it returns true when added row submited, which is good, but also when I click on eny existing cell even without editing it. How can I catch the exactly moment of submiting added row?

    J 1 Reply Last reply 24 Oct 2018, 09:03
    0
    • K Kot Shrodingera
      24 Oct 2018, 08:33

      I am subclassing QSqlTableModel (QSqlRelationalModel to be presize), editStrategy is OnFieldChange, dbdriver is QSQLITE. Table structure has some NOT NULL fields. Also I have QTableView and button that calls model->insertRows(model->rowCount()). When it is clicked, new row is inserted in view with asterisk(*) instead of row number (as I understand it, because NOT NULL fields aren't defined), when i define all NOT NULL fields and press Enter, asterisk changing to valid row number, so record is succesfully inserted into database. I want to have signal when this is happening. QAbstractTableModel::rowsInserted is called when empty row is added, so it's not suitable. I tried to catch QSqlTableModel::submit() method and its return value: it returns true when added row submited, which is good, but also when I click on eny existing cell even without editing it. How can I catch the exactly moment of submiting added row?

      J Offline
      J Offline
      JonB
      wrote on 24 Oct 2018, 09:03 last edited by
      #2

      @Kot-Shrodingera
      In your case, what about signals http://doc.qt.io/qt-5/qsqltablemodel.html#primeInsert or inherited http://doc.qt.io/qt-5/qabstractitemmodel.html#rowsInserted ?

      K 1 Reply Last reply 24 Oct 2018, 09:15
      0
      • J JonB
        24 Oct 2018, 09:03

        @Kot-Shrodingera
        In your case, what about signals http://doc.qt.io/qt-5/qsqltablemodel.html#primeInsert or inherited http://doc.qt.io/qt-5/qabstractitemmodel.html#rowsInserted ?

        K Offline
        K Offline
        Kot Shrodingera
        wrote on 24 Oct 2018, 09:15 last edited by Kot Shrodingera
        #3

        @JonB
        As I wrote, these signals are emmited when empty row inserted in model, but not submited to database. I want signal that emmits when all nesessary data for new row is entered , i. e. when asterisk changed to row number

        J 1 Reply Last reply 24 Oct 2018, 09:47
        0
        • K Kot Shrodingera
          24 Oct 2018, 09:15

          @JonB
          As I wrote, these signals are emmited when empty row inserted in model, but not submited to database. I want signal that emmits when all nesessary data for new row is entered , i. e. when asterisk changed to row number

          J Offline
          J Offline
          JonB
          wrote on 24 Oct 2018, 09:47 last edited by
          #4

          @Kot-Shrodingera
          In that case, I see only http://doc.qt.io/qt-5/qsqltablemodel.html#submit signal triggered. So your logic for that needs to distinguish the "new insert" from the "existing update" case, in some shape or form.

          If that proves difficult: in my own code (I use MySQL, not SQLite) I subclass and override all necessary methods of QSqlTableModel so that I can see what is actually happening when. Use the low-level, virtual protected function http://doc.qt.io/qt-5/qsqltablemodel.html#insertRowIntoTable to monitor the actual INSERT statement to the database?

          Also, if it were me, I would want to know whether your "submit returns true when I click on eny existing cell even without editing it" is actually submitting a SQL query to the database or not, because if it is that's not good....

          K 1 Reply Last reply 24 Oct 2018, 10:17
          1
          • J JonB
            24 Oct 2018, 09:47

            @Kot-Shrodingera
            In that case, I see only http://doc.qt.io/qt-5/qsqltablemodel.html#submit signal triggered. So your logic for that needs to distinguish the "new insert" from the "existing update" case, in some shape or form.

            If that proves difficult: in my own code (I use MySQL, not SQLite) I subclass and override all necessary methods of QSqlTableModel so that I can see what is actually happening when. Use the low-level, virtual protected function http://doc.qt.io/qt-5/qsqltablemodel.html#insertRowIntoTable to monitor the actual INSERT statement to the database?

            Also, if it were me, I would want to know whether your "submit returns true when I click on eny existing cell even without editing it" is actually submitting a SQL query to the database or not, because if it is that's not good....

            K Offline
            K Offline
            Kot Shrodingera
            wrote on 24 Oct 2018, 10:17 last edited by
            #5

            @JonB
            insertRowIntoTable is called even on empty row insert, but returns false. When field are set and Enter is pressed, method is called again and returns true. I think, I can work with this, thank you very much

            1 Reply Last reply
            1
            • V Offline
              V Offline
              VRonin
              wrote on 24 Oct 2018, 10:47 last edited by VRonin
              #6

              You can use the InsertProxyModel of this library to achieve the extra row insertion. You can find 2 examples using 2 different ways of submitting your row here

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              J 1 Reply Last reply 24 Oct 2018, 13:13
              1
              • V VRonin
                24 Oct 2018, 10:47

                You can use the InsertProxyModel of this library to achieve the extra row insertion. You can find 2 examples using 2 different ways of submitting your row here

                J Offline
                J Offline
                JonB
                wrote on 24 Oct 2018, 13:13 last edited by
                #7

                @VRonin
                As you know, I am always very respectful of the work you put in... :)

                In this case: since I personally have sub-classed QSqlTableModel and overridden insertRowIntoTable() with about 4 lines of code (and other methods), and it successfully does all that is needed for monitoring/knowing when a row is inserted, would you care to summarise in a couple of lines what your code achieves over that?

                V 1 Reply Last reply 24 Oct 2018, 13:19
                1
                • J JonB
                  24 Oct 2018, 13:13

                  @VRonin
                  As you know, I am always very respectful of the work you put in... :)

                  In this case: since I personally have sub-classed QSqlTableModel and overridden insertRowIntoTable() with about 4 lines of code (and other methods), and it successfully does all that is needed for monitoring/knowing when a row is inserted, would you care to summarise in a couple of lines what your code achieves over that?

                  V Offline
                  V Offline
                  VRonin
                  wrote on 24 Oct 2018, 13:19 last edited by
                  #8

                  @JonB said in QSqlTableModel signal on insert row submit:

                  would you care to summarise in a couple of lines what your code achieves over that?

                  I might well have misunderstood the question.
                  What I thought the need is, is for the view showing an extra row where the user could enter at least the data for the columns marked as NOT NULL before the row gets actually pushed to the SQL table.

                  My library has a proxy model that adds that extra row and let's you decide when a row becomes "acceptable" to be submitted to the main model

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  J 1 Reply Last reply 24 Oct 2018, 13:28
                  1
                  • V VRonin
                    24 Oct 2018, 13:19

                    @JonB said in QSqlTableModel signal on insert row submit:

                    would you care to summarise in a couple of lines what your code achieves over that?

                    I might well have misunderstood the question.
                    What I thought the need is, is for the view showing an extra row where the user could enter at least the data for the columns marked as NOT NULL before the row gets actually pushed to the SQL table.

                    My library has a proxy model that adds that extra row and let's you decide when a row becomes "acceptable" to be submitted to the main model

                    J Offline
                    J Offline
                    JonB
                    wrote on 24 Oct 2018, 13:28 last edited by JonB
                    #9

                    @VRonin
                    Ah. I believe the user described the interface he gets currently (with his QTableView). Whether he likes it or not is a different matter.

                    Then his question was: how does he get a notification when, having created a new row, it is actually sent to the database for INSERT, given that there is no signal for this in his case ("editStrategy is OnFieldChange").

                    My overriding QSqlTableModel::insertRowIntoTable() is how to achieve that. So yours is to do with the interface for adding a new row, right? Whether he would benefit/prefer to change over to that to I cannot say :)

                    1 Reply Last reply
                    1

                    9/9

                    24 Oct 2018, 13:28

                    • Login

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