PDA

View Full Version : Same model different views QTableView



Jarrod
11th August 2016, 22:04
Hi All,

How can I use the same model but only use some of the data/columns in it?
For example, Lets say the first column is "Name" and the second and third columns are "Number 1" and "Number 2" respectively.

How can I display a second view that keeps the Name but adds the 2 numbers together and displays it?
Is there a simple way of doing this?

Model 1:
Name| No1 | No2
Bob | 1 | 2

Model 2:
Name| Total
Bob | 3

Thanks.

d_stranz
12th August 2016, 05:49
Use a QSortFilterProxyModel (or a custom proxy model derived from QAbstractProxyModel) on your second view, with your original model set as its source model.

In the proxy, you will filter out the two columns whose values you want to add, and substitute a new column with the sum. This means you'll probably have to implement columnCount(), data, headerData(), and maybe filterAcceptsColumn() to exclude the two source columns you are going to add together.

I don't have a good example to give you, but there are plenty of variations if you google for it.