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? :)
Code:
ui->treeView->setSortingEnabled(true);
ui->treeView->sortByColumn(0, Qt::DescendingOrder);
Re: Problem with sorting QTreeView
Quote:
Originally Posted by
Mohsin
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.
1 Attachment(s)
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.Attachment 13634
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:
Quote:
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.