PDA

View Full Version : Sorting TreeView skip certain rows



pobrien11
11th April 2009, 02:01
Hi,

I have a treeView using a custom model. I want to sort on certain levels of the tree. For example, lets say level 1 is fruits, level 2 is apples, and level 3 is types of apples. I want to skip sorting on level 3 (types of apples). My level 3 has a lot of items and it causes a decent pause when Qt tries to sort them. Is this possible? How would you do it? I'm using QSortFilterProxyModel, but re-implementing lessThan doesn't help because the sort still goes through all the items. Using QT4.4. Thanks.

caduel
11th April 2009, 08:44
reimplement lessThan to return



static bool on_apple_level(const QModelIndex &idx)
{
// assuing apples (and only apples) are on the 3rd level
return idx.parent().parent().isValid();
}

bool yourModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
if (on_apple_level(left))
return left.row() < right.row();

// other comparisons
}

HTH

faldzip
11th April 2009, 15:17
if you are using your custom model you can reimplement it's QAbstractItemModel::sort() method, which can sort your data structure, which you use inside the custom model to store data. So in that method you can implement sorting in your own way, for example to skip sorting the 3. level. But this is sorting internal source model data structure, which is very different from proxy model approach , where the source model stays unchanged.

pobrien11
13th April 2009, 17:17
Thanks! I went ahead and tried the less than implementation and by doing what you suggested it works farely well. There is a slight pause, but much better than before.