Results 1 to 4 of 4

Thread: Problem with sorting QTreeView

  1. #1
    Join Date
    Apr 2021
    Posts
    27
    Thanks
    5

    Default Problem with sorting QTreeView

    After populating the QTreeView, I am using the following commands to sort the tree.

    However, nothing changes in QTreeView.

    Can anybody tell me what might be the issue?

    Qt Code:
    1. ui->treeView->setSortingEnabled(true);
    2. ui->treeView->sortByColumn(0, Qt::DescendingOrder);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with sorting QTreeView

    Quote Originally Posted by Mohsin View Post
    Can anybody tell me what might be the issue?
    Perhaps they were already in order. Perhaps the model does not support sorting.
    Hard to tell... we cannot see your code, your data, or what it looked like before and after sorting etc.

  3. #3
    Join Date
    Apr 2021
    Posts
    27
    Thanks
    5

    Default Re: Problem with sorting QTreeView

    I am using the Simple Tree Model code example. https://het.as.utexas.edu/HET/Softwa...treemodel.html

    And I have attached the screenshot of my data.Capture.PNG

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with sorting QTreeView

    You are using a basic implementation of a tree model. You will notice that it derives from QAbstractItemModel and that it does not override the QAbstractItemModel::sort() method. From the docs you will see:
    void QAbstractItemModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)

    Sorts the model by column in the given order.

    The base class implementation does nothing.
    No sorting ability is what you should expect from this model.

    You could extend the custom tree model to provide sorting ability that the view can use directly (harder to get right), or you can insert a QSortFilterProxyModel between your model and the view. There are good examples in the class documentation for the proxy model approach.

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

    Mohsin (29th April 2021)

Similar Threads

  1. Replies: 1
    Last Post: 15th February 2012, 15:56
  2. QTreeView sorting
    By Jonny174 in forum Qt Programming
    Replies: 2
    Last Post: 3rd May 2011, 09:24
  3. QTableView sorting problem
    By noktus in forum Newbie
    Replies: 11
    Last Post: 23rd April 2008, 11:20
  4. sorting problem in QSetting set.allkeys() ??
    By tech4web in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2008, 01:39
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 21:16

Tags for this Thread

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.