PDA

View Full Version : sorting qsqltable on multiple columns



dortegat
6th March 2012, 16:01
Is there any way in Qt 4.8 to sort by multiple columns one and QSqlTableModel or QSqlRelationalTableModel and display them in a TableView?

ChrisW67
6th March 2012, 23:08
One approach is to place a customised QSortFilterProxyModel between the base model and the view.

d_stranz
7th March 2012, 03:09
One approach is to place a customised QSortFilterProxyModel between the base model and the view.

So to implement a multi-column sort within a proxy model, would you reimplement QSortFilterProxyModel::lessThan() in such a way that the primary column is the one on which the model is sorted, but then within lessThan(), you expand this to also look at the model indexes for the secondary, etc. columns?

For example, if the model had columns for last name and first name, and you wanted to sort by first within last names, lessThan would first compare the left and right indexes (both of which point to entries in the last name column); if they were equal, then it would look at the corresponding indexes for the first name column.

This would see to me to be the least invasive way of implementing a multi-level sort. Is there a better way?