Results 1 to 9 of 9

Thread: Sorting column in QTreeWidget

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Sorting column in QTreeWidget

    Hi,

    My column contains numerical values (1, 2, 3, ..., 99, 100,...)

    When sort that column, the order is (1, 10, 100, 101, ..., 11, 12,...). Is there a way to sort in numerical order instead of alphabetic order?

    Thanks.

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting column in QTreeWidget

    The only way I know of is to go for QTreeView and a Model, where the implementation of the method data() returns a QVariant containing an int or long.

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

    Default Re: Sorting column in QTreeWidget

    Quote Originally Posted by DeepDiver View Post
    The only way I know of is to go for QTreeView and a Model, where the implementation of the method data() returns a QVariant containing an int or long.
    You still have a lot to learn before becoming a Master, young padawan

    Quote Originally Posted by lni View Post
    When sort that column, the order is (1, 10, 100, 101, ..., 11, 12,...). Is there a way to sort in numerical order instead of alphabetic order?
    Yes, subclass QTreeWidgetItem and reimplement its operator < to do the comparison based on numbers and not texts. You will probably also need to access the QTreeWidgetItem::treeWidget member to check which column is the active sorting column (QTreeWidget::sortColumn).

  4. The following user says thank you to wysota for this useful post:

    metdos (25th September 2009)

  5. #4
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting column in QTreeWidget

    Quote Originally Posted by DeepDiver View Post
    The only way I know of is to go for QTreeView and a Model, where the implementation of the method data() returns a QVariant containing an int or long.
    This is very bad. My design is based on QTreeWidget and QTreeWidgetItem.

    I do see QTreeWidgetItem::setData ( int column, int role, const QVariant & value )

    When using this method, I pass "int" into the QVariant, the ordering is still based on alphabetic...So I wonder if QTreeView and a Model will do the trick?

    Or do you mean I need to do the sorting myself in the data() method? That is not acceptable either...

    Thanks

  6. #5
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting column in QTreeWidget

    Quote Originally Posted by wysota View Post
    You still have a lot to learn before becoming a Master, young padawan
    Yes - master! I'll be willing to learn!

  7. #6
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting column in QTreeWidget

    Quote Originally Posted by lni View Post
    When using this method, I pass "int" into the QVariant, the ordering is still based on alphabetic...So I wonder if QTreeView and a Model will do the trick?
    Look at the impl:
    Qt Code:
    1. bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
    2. {
    3. int column = view ? view->sortColumn() : 0;
    4. return text(column) < other.text(column);
    5. }
    To copy to clipboard, switch view to plain text mode 
    The operator is comparing the stringified data. That's why you get the column sorted alphabetic.

    With wysota's solution you just need to re-impl this method to do the comparison on basis of the real data.

  8. The following user says thank you to DeepDiver for this useful post:

    metdos (25th September 2009)

  9. #7
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting column in QTreeWidget

    Quote Originally Posted by wysota View Post
    Yes, subclass QTreeWidgetItem and reimplement its operator < to do the comparison based on numbers and not texts. You will probably also need to access the QTreeWidgetItem::treeWidget member to check which column is the active sorting column (QTreeWidget::sortColumn).
    A little better, but still tedious... I wish they will take a comparator in QTreeWidget... Otherwise my general purpose MyTreeItem is no longer general purpose

    But a least it seems to have a way...however, before I try it, can you please tell me which occurs first, that is, when user clicks column 3, will I get "3" as active column in the comparison operator? Just want to make sure I am not comparing the previous active column...

    Thanks...

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

    Default Re: Sorting column in QTreeWidget

    Everything will be ok. Just reimplement the operator. Resorting is caused by changing the sort column, not the other way round.

    About the "tedious" argument - it is you who chose the tedious path by using QTreeWidget. The class is meant to be used for really simple cases, so basing a large application around QTreeWidget might have been a design error.

    One more thing - you can implement sorting in a tree widget subclass yourself if you want. Just create a sorting slot and connect it to the header using signals.
    Last edited by wysota; 14th November 2007 at 18:07.

  11. #9
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting column in QTreeWidget

    Quote Originally Posted by lni View Post
    A little better, but still tedious... I wish they will take a comparator in QTreeWidget... Otherwise my general purpose MyTreeItem is no longer general purpose

    But a least it seems to have a way...however, before I try it, can you please tell me which occurs first, that is, when user clicks column 3, will I get "3" as active column in the comparison operator? Just want to make sure I am not comparing the previous active column...

    Thanks...
    Thanks. I got it done by subclassing QTreeWidgetItem already

    One observation I have is that I have to press mouse harder on the header section in order for the sorting to be triggered, otherwise it won't... Look like QTreeWidget have a new feature to detect the pressing force...


    Thanks guys!

Similar Threads

  1. Sorting in QTreeWidget
    By adhit in forum Qt Programming
    Replies: 15
    Last Post: 8th May 2007, 12:49
  2. QTreeWidget item editing: want column specificity
    By McKee in forum Qt Programming
    Replies: 12
    Last Post: 10th December 2006, 22:12
  3. Replies: 1
    Last Post: 21st September 2006, 10:37
  4. Column Sorting
    By sumsin in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 07:48
  5. QT4: Sorting in QTreeWidget (subclass)
    By Michiel in forum Qt Programming
    Replies: 21
    Last Post: 29th March 2006, 18:08

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.