Solved Any way a database model QTableView could have an extra "total" row?
-
I have
QTableView
s againstQSqlTable/QueryModel
models populated by straightforward SQLSELECT
s.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...?
-
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 aQSqlQueryModel
? 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).... -
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
QTableView
s have no option for a footer. The only way is to write a lot of code yourself, including fiddling with theQTableView
'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! :)
-
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 :) -
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 inQTableView
?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.
-
@StarterKit hi,
It depends a bit on what you want to show and how.
A proxy model could also be used.
-
-
@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. CertainlyQTableView
has not introduced a footer :) -
@JonB ok, thank you for your comment, it is ok.
My idea might be a bit strange... but I managed to add one moreQHeaderView
below the table, it looks good.
So I subclassed it and created my own FooterView class to handlepaintSection()
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 :) -
@StarterKit
Sounds good. I think mine was very specifically wanting an extra row at the bottom of the table rows, as an extra row.