Results 1 to 4 of 4

Thread: Sorting TreeView skip certain rows

  1. #1
    Join Date
    Apr 2009
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default Sorting TreeView skip certain rows

    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.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting TreeView skip certain rows

    reimplement lessThan to return
    Qt Code:
    1. static bool on_apple_level(const QModelIndex &idx)
    2. {
    3. // assuing apples (and only apples) are on the 3rd level
    4. return idx.parent().parent().isValid();
    5. }
    6.  
    7. bool yourModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
    8. {
    9. if (on_apple_level(left))
    10. return left.row() < right.row();
    11.  
    12. // other comparisons
    13. }
    To copy to clipboard, switch view to plain text mode 
    HTH

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sorting TreeView skip certain rows

    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.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Apr 2009
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default Re: Sorting TreeView skip certain rows

    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.

Similar Threads

  1. QTableWidget sorting with hidden rows
    By alex140773 in forum Qt Programming
    Replies: 0
    Last Post: 8th July 2008, 12:35
  2. Treeview scrollbar not updating when inserting rows
    By Khal Drogo in forum Qt Programming
    Replies: 11
    Last Post: 29th November 2007, 13:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.