PDA

View Full Version : QSortFilterProxyModel and QTreeView



PrimeCP
17th April 2009, 03:05
I need to do a slightly more complex sort on strings for a QTreeView. The 1st level nodes all need to be sorted by string but the sort needs to be more complex than normal alphabetical, I need to sort based off of sub strings . Here is a simple example:

Given strings: AA1, AA2, AB1, AB2

I need to sort based on first character; then last character; and then middle character. So to get the proper new sort order I would need the result to be:

AA1
AB1
AA2
AB2

The strings were sorted on the first character (A) using normal alphabetic, then sorted on the third character (the numbers) and lastly the middle character to get this result.

Hopefully that makes sense.

I'm not sure if I can use a QSortFilterProxyModel to do this character based sorting ONLY on the first level of nodes (not the children) in a QTreeView.

Thanks

aamer4yu
17th April 2009, 05:32
You can provide your custom comparison method by overriding QSortFilterProxyModel::lessThan :)

wysota
17th April 2009, 10:50
You can also add a custom role to your base model that will return the data in a format appropriate for sorting and use that role for sorting by calling QSortFilterProxyModel::setSortRole().