Results 1 to 4 of 4

Thread: QTreeWidget, setSortingEnabled

  1. #1
    Join Date
    Nov 2008
    Posts
    21
    Thanked 1 Time in 1 Post

    Default QTreeWidget, setSortingEnabled

    Hi,

    I'm using several QTreeWidgets and activated now the setSortingEnabled. I have some problems with that function:

    - when I enable the sorting, the widgets sorts automatically the first column in descending(!) order, why that? Can I change at least the default sort order to ascendng order?
    - How can I remove the sorting so the normal order (the order I inserted the elements) is visible again?
    - What would be the easiest way to sort a column that has text and numbers, it should sort the numbers like 1, 5, 10, 20 and not 1, 10, 20, 5

    I do not want to use QTreeView and implement my own models because the QTreeWidget is already used in many places and I do not want to change all that code.

    Thanks for any answer!

    Cheers,
    Rico

  2. #2
    Join Date
    Mar 2008
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    3
    Thanked 3 Times in 2 Posts

    Default Re: QTreeWidget, setSortingEnabled

    You can access to the header of QTreeView by calling header() then use QHeaderView::setSortIndicator ( int logicalIndex, Qt::SortOrder order ) to change the column and sort order. Or use QTreeView::sortByColumn( int column, Qt::SortOrder order )To remove sorting just set setSortingEnabled to false.
    By default QTreeWidget sort items by text. So if you want to sort by int you have to subclass QTreeWidgetItem and reimplement bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: QTreeWidget, setSortingEnabled

    Quote Originally Posted by osiris81 View Post
    - What would be the easiest way to sort a column that has text and numbers, it should sort the numbers like 1, 5, 10, 20 and not 1, 10, 20, 5
    Then set nembers to your items, then they are sorted like you want them to. E.g. don't use:
    Qt Code:
    1. item->setText(4, "5");
    To copy to clipboard, switch view to plain text mode 
    use
    Qt Code:
    1. item->setData(4,Qt::DisplayRole, 5);
    To copy to clipboard, switch view to plain text mode 

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

    Cupidvogel (30th April 2015)

  5. #4
    Join Date
    Nov 2008
    Posts
    21
    Thanked 1 Time in 1 Post

    Default Re: QTreeWidget, setSortingEnabled

    thanks a lot for your help, that helped me!

Similar Threads

  1. Qtreewidget
    By newermind in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2009, 11:09
  2. Replies: 2
    Last Post: 17th March 2008, 12:53
  3. Replies: 1
    Last Post: 19th October 2007, 01:29
  4. QTableWidget - setSortingEnabled
    By bruccutler in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2007, 15:27
  5. QTreeWidget
    By allensr in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2006, 16:24

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.