Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]T he description about sorting in Model/View Qt document maybe wrong?
Forum Update on Monday, May 27th 2025

[Solved]T he description about sorting in Model/View Qt document maybe wrong?

Scheduled Pinned Locked Moved General and Desktop
sortingmodel-viewqtreeview
3 Posts 2 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Enderson
    wrote on 20 Mar 2015, 02:35 last edited by Enderson
    #1

    In Qt document online Model/View Programming, it's said that If your model is sortable, i.e, if it reimplements the QAbstractItemModel::sort() function, both QTableView and QTreeView provide an API that allows you to sort your model data programmatically. In addition, you can enable interactive sorting (i.e. allowing the users to sort the data by clicking the view's headers), by connecting the QHeaderView::sortIndicatorChanged() signal to the QTableView::sortByColumn() slot or the QTreeView::sortByColumn() slot, respectively.
    However, firstly the QTableView::sortByColumn() is not a slot, so one cannot connect a signal to it; secondly, the code of QTableView::sortByColumn() is something like

    d->header->setSortIndicator(column, order);
    //If sorting is not enabled, force to sort now.
    if (!d->sortingEnabled)
        d->model->sort(column, order);
    

    and in QHeaderView::setSortIndicator() function emit sortIndicatorChanged(logicalIndex, order); but if uses function setSortingEnabled(true) in QTreeView, the signal sortIndicatorChanged(logicalIndex, order); can also be emitted automatically.

    So maybe the right way is to make a slot to receive the signal sortIndicatorChanged(logicalIndex, order); of the header, and in the slot, call the override virtual function sort() in the model.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tarod.net
      wrote on 20 Mar 2015, 10:49 last edited by
      #2

      It seems you're right.

      Maybe the explanation is not perfect.

      "Individually, we are one drop. Together, we are an ocean."

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Enderson
        wrote on 21 Mar 2015, 01:33 last edited by Enderson
        #3

        Sort the tree view by click a column.

        1. Set the view can be sorted by click the "header".

           treeView_->setSortingEnabled(true);
          
        2. Connect the header signal to a slot made by you.

           connect(treeView_->header(), SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)),
               treeModel_, SLOT(sortByColumn(int, Qt::SortOrder)));
          
        3. In the slot, call the sort() virtual function of the model. sort() virtual function is a virtual function of QAbstractItemModel, and one should override it.

           void TreeModel::sortByColumn(int column, Qt::SortOrder order)
           {
               sort(column, order);
           }
          
        4. Override the sort() function as your model should do.

        5. emit dataChanged(QModelIndex(), QModelIndex()); from a model to update the whole tree view.

        1 Reply Last reply
        1

        2/3

        20 Mar 2015, 10:49

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved