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. Any way a database model QTableView could have an extra "total" row?

Any way a database model QTableView could have an extra "total" row?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 2.3k Views 4 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.
  • JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #1

    I have QTableViews against QSqlTable/QueryModel models populated by straightforward SQL SELECTs.

    Users being users, some of them now want a "total" row at the bottom, for certain columns which contain numbers. (Doing the totalling at the Qt client side is fine, not looking to do a SUM() up at server and it wouldn't help anyway.)

    Is there any (not too large!) approach which could persuade the table view to have such a thing? It's not like I can "append" an extra row at the bottom by inserting an extra row into the model, as I understand it QSqlQueryModel is read-only and does no allow any rows to be added after the SQL result set has been returned. Trying to somehow put another "line" below the table view, which would probably be ugly anyway, will doubtless not line up nicely with the columns.

    Anyone got any suggested approaches...?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Derive from QSqlQueryModel, overwrite the data() and columnCount() and return your values for the last column() for example.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Derive from QSqlQueryModel, overwrite the data() and columnCount() and return your values for the last column() for example.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        Hmm, you know I'll always respect & investigate what you write!

        The first potential problem I see (I see many!): sometimes there is a QSortFilterProxyModel interposed in there to do sorting (and filtering). How am I going to make it so my total row always comes as the final row (not somewhere in the middle or excluded by the filter)?

        Also (while I think of it) is this going to work where I have a QSqlTableModel rather than a QSqlQueryModel? I can't recall quite what, but that really wants it to be a proper table... Let alone, the tables allow editing, I'm going to have deal with not letting the user edit the bottom row or treating it as having been inserted.....

        Would have been better if QTableView allowed a "footer" row I could maybe have put this in (yes, I'm already using the "header" row)....

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          You did not say that you want to sort :)
          Then I would try it with a QIdentityProxyModel chained after the QSortFilterProxyModel.

          /edit: and even when you want to sort you can set the sort role to something else: https://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          JonBJ 2 Replies Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            You did not say that you want to sort :)
            Then I would try it with a QIdentityProxyModel chained after the QSortFilterProxyModel.

            /edit: and even when you want to sort you can set the sort role to something else: https://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @Christian-Ehrlicher
            I did not say lots of things!

            I have a well-established working set of tables/models/views. They do lots of things: sorting, filtering, editing, special display of values, .... Now I want an extra row at the bottom, sometimes, so I can display totals in certain numeric columns. Other columns in that row will be blank. The first column may well need to display a "Total" text, even if that column normally displays something else. I want everything to work just as it did, and then a total row as well.

            Oh, and btw: if the code sorts now, I don't expect to have to "set the sort role to something else" to make something work. I expect the addition of the total row not to require much changes to existing code (other than my calculating it from the model, obviously).

            :)

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              You did not say that you want to sort :)
              Then I would try it with a QIdentityProxyModel chained after the QSortFilterProxyModel.

              /edit: and even when you want to sort you can set the sort role to something else: https://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @Christian-Ehrlicher
              The more I think about it, the more it seems to me what I really want is a "footer" like the header row, not as a row in the table, so I can put arbitrary text there like in the column headers. In my C# ASP.NET, tables had a (optional) footer as well as a header!

              I looked into this, and it seems that QTableViews have no option for a footer. The only way is to write a lot of code yourself, including fiddling with the QTableView's area to accommodate the footer, which is well beyond the scope for me here.

              1 Reply Last reply
              1
              • JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                Since it can't be done, I ended up making do, in my particular case, with an extra row in the model, using a QStandardItemModel.

                Not happy, but needs-must! :)

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

                  Hi,

                  Maybe the Frozen Column example might give you some ideas.

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

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    Maybe the Frozen Column example might give you some ideas.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

                    @SGaist
                    And so it might, for others who read this. For me, at least in this situation, anything more than about half a dozen lines is deemed "too complex/too much effort"! Multiple views, viewports, scrollers etc. is way too much for my situation.

                    Now, if Qt decides to add a (optional) footer to a QTableView in the way it has added a header, please let me know :)

                    S 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @SGaist
                      And so it might, for others who read this. For me, at least in this situation, anything more than about half a dozen lines is deemed "too complex/too much effort"! Multiple views, viewports, scrollers etc. is way too much for my situation.

                      Now, if Qt decides to add a (optional) footer to a QTableView in the way it has added a header, please let me know :)

                      S Offline
                      S Offline
                      StarterKit
                      wrote on last edited by
                      #10

                      Hi all,
                      Sorry for necro-posting but I don't think it deserves new separate topic.
                      Do I understand right that nothing have changed since 2019? I.e. we still have no good way to have a footer row in QTableView?

                      Basically a dozen code lines is what I was looking for... but I feel myself ready to make some extra class if required... but... I'm not sure that there is any good option to implement it rather than onces discussed above in this topic.

                      SGaistS 1 Reply Last reply
                      0
                      • S StarterKit

                        Hi all,
                        Sorry for necro-posting but I don't think it deserves new separate topic.
                        Do I understand right that nothing have changed since 2019? I.e. we still have no good way to have a footer row in QTableView?

                        Basically a dozen code lines is what I was looking for... but I feel myself ready to make some extra class if required... but... I'm not sure that there is any good option to implement it rather than onces discussed above in this topic.

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @StarterKit hi,

                        It depends a bit on what you want to show and how.

                        A proxy model could also be used.

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

                        S 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          @StarterKit hi,

                          It depends a bit on what you want to show and how.

                          A proxy model could also be used.

                          S Offline
                          S Offline
                          StarterKit
                          wrote on last edited by
                          #12

                          @SGaist I need exactly the same as @JonB described.
                          I use a working application with tables/models/views and I need to add a footer to a couple of QTableViews that would be able to show total sum of this column.

                          JonBJ 1 Reply Last reply
                          0
                          • S StarterKit

                            @SGaist I need exactly the same as @JonB described.
                            I use a working application with tables/models/views and I need to add a footer to a couple of QTableViews that would be able to show total sum of this column.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #13

                            @StarterKit
                            I don't recall my exact situation or what I understood then, but knowing what I do now I only know there is just adding a (identity) proxy model appending one more row. It would have to come on top of any sort proxy. Certainly QTableView has not introduced a footer :)

                            S 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @StarterKit
                              I don't recall my exact situation or what I understood then, but knowing what I do now I only know there is just adding a (identity) proxy model appending one more row. It would have to come on top of any sort proxy. Certainly QTableView has not introduced a footer :)

                              S Offline
                              S Offline
                              StarterKit
                              wrote on last edited by StarterKit
                              #14

                              @JonB ok, thank you for your comment, it is ok.
                              My idea might be a bit strange... but I managed to add one more QHeaderView below the table, it looks good.
                              So I subclassed it and created my own FooterView class to handle paintSection() event and provide data from the model with custom User-role.
                              I don't need fancy UI style and column movments so I think I'll survive with this solution :)

                              JonBJ 1 Reply Last reply
                              0
                              • S StarterKit

                                @JonB ok, thank you for your comment, it is ok.
                                My idea might be a bit strange... but I managed to add one more QHeaderView below the table, it looks good.
                                So I subclassed it and created my own FooterView class to handle paintSection() event and provide data from the model with custom User-role.
                                I don't need fancy UI style and column movments so I think I'll survive with this solution :)

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by
                                #15

                                @StarterKit
                                Sounds good. I think mine was very specifically wanting an extra row at the bottom of the table rows, as an extra row.

                                1 Reply Last reply
                                1

                                • Login

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