Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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...?


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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



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

    :)



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



  • 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! :)


  • Lifetime Qt Champion

    Hi,

    Maybe the Frozen Column example might give you some ideas.



  • @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 :)


Log in to reply