Results 1 to 16 of 16

Thread: Sorting in QTreeWidget

  1. #1
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Sorting in QTreeWidget

    Hello!

    QTreeWidget class has a function setSortingEnabled(bool), which provides us to sort items by clicking mouse button on the widget headers.
    Does anybody know, how can I catch this event? When the user clicks mouse on the tree header I want to catch this event, and determine the sorting order. And then I will call my function, which will handle this event.

    In short, how can I catch this event and define the sorting order?

  2. #2
    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 in QTreeWidget

    You should take the model approach and reimplement the sort() method in the model.

  3. #3
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting in QTreeWidget

    Quote Originally Posted by wysota View Post
    You should take the model approach and reimplement the sort() method in the model.
    Wysota, can yo tell me, how can I take the model approach?
    Thanks!

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting in QTreeWidget

    QTreeWidget inherits QAbstractItemView, which has a method setModel( QAbstractItemModel * ).

    You must subclass QAbstractItemModel and ( among others ) override its sort(...) method. Or maybe, depending on your needs you can subclass QStandardItemModel.

    When you're done, use your model and set it using setModel.

    Regards.

  5. The following user says thank you to marcel for this useful post:

    adhit (6th May 2007)

  6. #5
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting in QTreeWidget

    Marcel, but setModel(..) in QTreeWidget class is private.
    Can I use QTreeView::setModel(..)?

    So, you mean, that I need to create my own model with my own sort in this model, and then set this model to my tree?

    Thanks!

  7. #6
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Sorting in QTreeWidget

    Quote Originally Posted by adhit View Post
    Marcel, but setModel(..) in QTreeWidget class is private.
    Can I use QTreeView::setModel(..)?
    You could use QTreeView, but there is no need for that in your case. Especially there is no need for overriding setModel().


    Quote Originally Posted by adhit View Post
    So, you mean, that I need to create my own model with my own sort in this model, and then set this model to my tree?
    Right.

    Regards Ben

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting in QTreeWidget

    What do you mean it is private?
    virtual void setModel ( QAbstractItemModel * model ) is public in QAbstractItemView, therefore public in QTreeVeiw and public in QTreeWidget.

    So, you mean, that I need to create my own model with my own sort in this model, and then set this model to my tree?
    Yes, you need to create your own mode and override the sort method.

    Regards

  9. #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 in QTreeWidget

    Quote Originally Posted by marcel View Post
    You must subclass QAbstractItemModel and ( among others ) override its sort(...) method. Or maybe, depending on your needs you can subclass QStandardItemModel.
    Go for QStandardItemModel or even use a proxy model and do the sorting there.


    Quote Originally Posted by adhit View Post
    Marcel, but setModel(..) in QTreeWidget class is private.
    Can I use QTreeView::setModel(..)?
    No. You have to use QTreeView instead of QTreeWidget. QTreeWidget has an internal model and trying to set another model won't result in anything good.

    So, you mean, that I need to create my own model with my own sort in this model, and then set this model to my tree?
    Correct. Just make sure to emit proper signals (probably layoutChanged()) from within sort().

  10. #9
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: Sorting in QTreeWidget

    For example, I have:

    // Class of new model
    class TreeModel : public QStandardItemModel
    {
    Q_OBJECT
    public:
    TreeModel( QObject *parent = 0 );
    protected:
    void sort( int, Qt::SortOrder );
    };

    // class of the tree
    class XMLTree : public QTreeWidget
    {
    Q_OBJECT
    public:
    XMLTree( QWidget *parent = 0 );
    ...
    private:
    TreeModel *_model;
    ...
    };

    TreeModel::TreeModel( QObject *parent )
    :QStandardItemModel( parent )
    {
    }

    void TreeModel::sort( int col, Qt::SortOrder order )
    {
    // My sorting
    }

    XMLTree::XMLTree( QWidget *parent )
    :QTreeWidget( parent )
    {
    _model = new TreeModel( model() );
    // ???
    // How can I copy standard tree model properties to my model?
    // I mean, that my model should be like standard model, but
    // only with my sorting.
    QTreeView::setModel( _model );
    ...
    }

    No. You have to use QTreeView instead of QTreeWidget. QTreeWidget has an internal model and trying to set another model won't result in anything good.
    And... If I can't set new model to QTreeWidget, how can I override sorting in this situation?

    Thanks!!!

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Sorting in QTreeWidget

    You can do it fairly easily with the convenience view as well:
    Qt Code:
    1. class MyTreeWidget : public QTreeWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyTreeWidget(QWidget* parent = 0)
    7. : QTreeWidget(parent)
    8. {
    9. // disable built-in sorting
    10. setSortingEnabled(false);
    11.  
    12. // use our own sorting method instead
    13. header()->setSortIndicatorShown(true);
    14. header()->setClickable(true);
    15. connect(header(), SIGNAL(sectionClicked(int)), this, SLOT(customSortByColumn(int)));
    16. customSortByColumn(header()->sortIndicatorSection());
    17. }
    18.  
    19. public slots:
    20. void customSortByColumn(int column)
    21. {
    22. // here you can get the order
    23. Qt::SortOrder order = header()->sortIndicatorOrder();
    24.  
    25. // and sort the items
    26. sortItems(column, order);
    27.  
    28. // to get more control over actual sorting of items,
    29. // reimplement QTreeWidgetItem::operator<()
    30. }
    31. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  12. #11
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: Sorting in QTreeWidget

    jpn, THANK YOU VERY MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  13. #12
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting in QTreeWidget

    thank All Of You For Help!!!
    Denis Davidov

  14. #13
    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 in QTreeWidget

    Please don't shout. Thank you.

  15. #14
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting in QTreeWidget

    Hello, everyone!

    I have parent item in the tree. This item has a list of children (QList<QTreeWidgetItem *>). How can I change the order of children in this list? I mean, how can I change the order manually, without using sortChildren() or clone()->takeChild()->insertChild().

    Any ideas...
    Denis Davidov

  16. #15
    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 in QTreeWidget

    You don't need clone(). Just take the item and insert it elsewhere (I think you needn't even call takeTopLevelItem). I don't think there is a way to do it without taking items out (you can use qSwap() on dereferrenced pointers to exchange attributes of items, but then you'll have to implement the assignment operator properly).

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

    adhit (8th May 2007)

  18. #16
    Join Date
    Apr 2007
    Location
    Moscow, Russia
    Posts
    20
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting in QTreeWidget

    wysota, thanks!

    takeItem() - that's cool! At first, I used it only to delete item from the tree. But It also can take item and insert it in the new position.
    Denis Davidov

Similar Threads

  1. QTreeWidget drag/drop
    By s_a_white in forum Newbie
    Replies: 1
    Last Post: 10th February 2007, 22:04
  2. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32
  3. QT4: Sorting in QTreeWidget (subclass)
    By Michiel in forum Qt Programming
    Replies: 21
    Last Post: 29th March 2006, 18:08
  4. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32

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.