Results 1 to 3 of 3

Thread: Sorting QTreeWidget after adding a row

  1. #1
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Sorting QTreeWidget after adding a row

    Hello,

    I’m programmatically sorting a QTreeWidget using sortItems(int column, Qt::SortOrder order). It works, of course. However, if I do it right after adding some items to the tree, the sort won’t take effect.

    I’ve even tried calling sortItems from a one-shot QTimer. This works a little bit better, but still fails 50% of the time. Clearly something needs to be “updated” or validated in the QTreeWidget before the sort will work.

    Is there a way to force the QTreeWidget to update so I can call sortItems immediately?

    Many thanks.

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

    Default Re: Sorting QTreeWidget after adding a row

    how and where do you call the sort function and how for example do you add a new widget? The error is most probably in the order of calling these functions.

  3. #3
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Sorting QTreeWidget after adding a row

    Hi there, I'm doing things like this:
    Qt Code:
    1. QTreeWidgetItem *newItem = new QTreeWidgetItem("Foo");
    2. theTree->addTopLevelItem(newItem);
    3.  
    4. newItem = new QTreeWidgetItem("Bar");
    5. theTree->addTopLevelItem(newItem);
    6.  
    7. // etc. etc....
    8.  
    9. // after all items have been added, we then call the following within the same function:
    10. theTree->sortItems(0, Qt::AscendingOrder);
    To copy to clipboard, switch view to plain text mode 

    Of course things are a little more complicated than that, with multiple columns etc. But this is how things are added, sorted, and in the order that it happens. I wouldn't think it could be any simpler than this!

    Running Qt 4.6.2 if that makes any difference.

Similar Threads

  1. Dynamic sorting using proper column after adding a row.
    By kremuwa in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2010, 23:50
  2. Sorting column in QTreeWidget
    By lni in forum Qt Programming
    Replies: 8
    Last Post: 14th November 2007, 18:35
  3. QTreeWidget Weights/Sorting
    By VireX in forum Qt Programming
    Replies: 27
    Last Post: 24th May 2007, 23:49
  4. Sorting in QTreeWidget
    By adhit in forum Qt Programming
    Replies: 15
    Last Post: 8th May 2007, 12:49
  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.