PDA

View Full Version : QSqlTableModel and QTableView and sorting



JeanC
4th April 2008, 16:02
Hello,

I have setSortingEnabled (true) for this table so that when I click on a column header, the table gets sorted to that column. Very nice.

But in combination with setFilter() the sorting order is not always the same. The order of records is different if a filter is applied.

There are columns with the same value, and in a normal sql case you would then use an 'order by' with 2 arguments, 'order by thisfirst, thatsecond' which would get around such problems. I suspect this is the reason why the order differs when I set a filter.

Trying to illustrate:

Name Type Age (sorted on column Age)
a 1 1
b 1 1
c 2 1

setFilter(type = 1)

b 1 1
a 1 1

Should be a first, then b.

Hope I am being a bit clear. Thanks.

JeanC
5th April 2008, 13:22
It looks like there is no easy solution to this.
Just as sortByColumn() only accepts 1 column (as opposed to 'order by one,two').

A workaround is to have all fields in the column that you want to sort on have
unique values, this way if you setFilter() the sortorder is the same as without filter.
Of course this is not alwaws desirable or possible.