I tested this code with QStandardItemModel and it works fine, lessThan is called properly so I think that problem is in my CustomTableModel. Maybe I forgot about some important function which should be reimplemented?
I tested this code with QStandardItemModel and it works fine, lessThan is called properly so I think that problem is in my CustomTableModel. Maybe I forgot about some important function which should be reimplemented?
Very stupid mistake =-=. And you can see it in my code, exactly in mainwindow.h.
First I tested sorting data with QSortFilterProxyModel and when I wrote my CustomSortFilterProxyModel I forgot to change the definitions of these classes in mainwindow.h....
Sorry for that.
But I have one more question - it is a good way for sort table by multiple column? or in Qt we have better way for this?
You can always sort inside your model.
I usually prefer that over using a proxy if the sorting gets more complicated.
Cheers,
_
QSortFilterProxyModel only supports sorting on a single column. You could probably write a lessThan() method that would do a multi-level sort for a custom QSortFilterProxyModel, but the proxy is still going to maintain its state under the assumption that it is a single-column sort. As anda_skoa says, it is usually easier to do this in the source model if the sorting is complex.But I have one more question - it is a good way for sort table by multiple column?
There is a reason for the focus on single-column sorting. If each sort is stable (which is usually expected in a UI), then you can get the lexicographic order on columns c1..cn by sorting by cn, then cn-1, ..., then c1. E.g. to sort files by date, then name, you can first click on the "name" column, then on the "date" column.
I don't think that works in a custom model (or even in the off-the-shelf QSortFilterProxyModel) - unless the model (or proxy) implements such multi-column sort behavior. How does a standard QSortFilterProxyModel know to maintain the sort on one column that contains duplicates while sorting a second column with respect to those duplicates? As far as I have seen, sorting on any column occurs independently of other columns and results in all rows being reordered solely on the sort order of the chosen column.to sort files by date, then name, you can first click on the "name" column, then on the "date" column.
Bookmarks