Results 1 to 20 of 28

Thread: QTreeWidget Weights/Sorting

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default QTreeWidget Weights/Sorting

    I am trying to sort my QTreeWidget, but I was wondering if there was a way to add Weights to the topLevelnodes. I mean I don't want people to sort it by say "name" column, because then it sorts my top level nodes, and the order becomes messed up.

    What I want to do is keep top level nodes in a fixed order. But I want their children to be sorted normally by "name" column (which has everything).

    Can I do this without subclassing and overriding sort()?
    Perhaps install an event filter?
    Or Is there some sort of "Weight" property I can add (Didn't see) ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeWidget Weights/Sorting

    Reimplement bool operator< ( const QTreeWidgetItem & other ) const for your tree items and use the created subclass instead of QTreeWidgetItem.

  3. #3
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QTreeWidget Weights/Sorting

    Ok I reimplemented that in this way. However, will this sort "names" as well in alphabetical even if I override the original < operator? Or should I code that in too?

    Qt Code:
    1. #include "XTreeItem.h"
    2.  
    3. bool QTreeWidgetItem::operator< ( const QTreeWidgetItem & other ) const {
    4. int iScore, iMyScore;
    5. iScore = 0;
    6. iMyScore = 0;
    7. if(other.flags() == 0){
    8. if(other.text(0) == QString("FIRST"))iScore = 100;
    9. if(other.text(0) == QString("SECOND"))iScore = 90;
    10. if(other.text(0) == QString("THIRD"))iScore = 80;
    11. if(other.text(0) == QString("FOURTH"))iScore = 70;
    12. if(other.text(0) == QString("FIFTH"))iScore = 60;
    13. if(other.text(0) == QString("SIXTH"))iScore = 50;
    14. }
    15. if(flags() == 0){
    16. if(text(0) == QString("FIRST"))iMyScore = 100;
    17. if(text(0) == QString("SECOND"))iMyScore = 90;
    18. if(text(0) == QString("THIRD"))iMyScore = 80;
    19. if(text(0) == QString("FOURTH"))iMyScore = 70;
    20. if(text(0) == QString("FIFTH"))iMyScore = 60;
    21. if(text(0) == QString("SIXTH"))iMyScore = 50;
    22. }
    23. if(iMyScore < iScore){ return true; } else { return false; }
    24. }
    25.  
    26. #include "XTreeItem.moc"
    To copy to clipboard, switch view to plain text mode 

    I know I shoulda used switches and/or functions better (more OO) but still is this basically what I should do to add weights?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeWidget Weights/Sorting

    What original operator? You have to subclass and reimplement the method in the subclass.

  5. #5
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QTreeWidget Weights/Sorting

    That's what I did, is the code correct?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeWidget Weights/Sorting

    Qt Code:
    1. bool QTreeWidgetItem::operator< ( const QTreeWidgetItem & other ) const
    To copy to clipboard, switch view to plain text mode 
    This doesn't look like a subclass.

  7. #7
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QTreeWidget Weights/Sorting

    Because I forgot to change the name of the class, but yes, assume it is a subclass. You still have not commented on the actual code?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeWidget Weights/Sorting

    It's hard to understand it... Besides, it doesn't do much sorting... For instance it doesn't seem to compare values of items at all... Only their "flags". Is "flags" the only property of your items?

  9. #9
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QTreeWidget Weights/Sorting

    Well my toplevel nodes have 0 flags, so its only way to detect them, the rest have flags. And I'm assuming Qt will sort the rest by default (in other words my function and original function working together)

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeWidget Weights/Sorting

    Quote Originally Posted by VireX View Post
    Well my toplevel nodes have 0 flags, so its only way to detect them, the rest have flags.
    No. Top level nodes don't have parents. That's a way to detect them.

    And I'm assuming Qt will sort the rest by default (in other words my function and original function working together)
    But you never call the base class implementation so only your method gets called. That's how virtual methods work.

  11. #11
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QTreeWidget Weights/Sorting

    So how do I call it...?

    Do I call it like this:
    bool XTreeItem:perator< ( const QTreeWidgetItem & other ) const : QTreeWidgetItem:perator< (other) {
    Last edited by VireX; 21st May 2007 at 21:03.

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
  •  
Qt is a trademark of The Qt Company.