Is it possible to sort on multiple columns in QTableView using QSortFilterProxyModel
Is it possible to sort on multiple columns in QTableView using QSortFilterProxyModel? at least programmatically.
The examples show sorting on one column, which is achieved by enabling sorting on the view and then later, implementing our own lessThan function in QSortFilterProxyModel.
I want to know if there is a way to achieve multiple column sort using the MVC classes.
AFAIK, you might have to extend QSortFilterProxyModel and build this capability of sorting on multiple columns.
There is a workaround, but not optimal in terms of performance ...
If you want to sort by Col A and then by Col B, then first sort by Col B and then sort by Col A... it should work. :)
Thanks chetankjain. I think this works...
but by default they don't support multiple column sort like other popular frameworks which provide tables :( I think this has to be included in the future.
glad it worked, developing this component would be a good exercise :)
It is not hard to make a generic QSortFilterProxy subclass that can sort on multiple columns and that can be applied generically. If you think it would be useful, please write one and share it, either by trying to merge it into Qt or by just sharing the component via your own website or one of the community sites like qt-apps.org.
I found this code that is supposed to work for mutliple column filtering.
Maybe it could be integrated in QSortFilterProxyModel?
Yet i'm still having trouble using this code. i'll try to code my own class so I can understand better why my implementation doesn't work
I found this line of code was important :
every time the filter change, call that :)
ekoswibowo last edited by
Can we do multi column sort using QTableWidget? I would love to keep my custom widget subclassed from it.. I think I will rework a lot of works if I have to change it from QTableView
I don't think so, no. You'd have to use a custom QSortFilterProxyModel, but I don't think you can make sure that the QTableWidget's internal model will stay alive once you set another model on it. My personal view is that the widget versions of the views were a mistake to introduce in the first place.
I think the easiest way to port is to use QStandardItemModel instead, and use a QTabelView instead of a QTableModel. QStandardItemModel has an API that is very similar to the data related API of QTableWidget, so that should not be too big an issue. You can then use whatever proxy model you need to do your advanced sorting.