There is a very simple tree model example in Qt document. I added a line
view.setSortingEnabled(true);
in main.cpp to enable the tree view sorting. When I run it, I see a sorting indicator in the header. However, no matter how I click the header, the items are not sorted. Is there anything I ignored?
Use QSortFilterProxyModel with your view.
The view's sortingEnabled property enables the sort buttons in the view. However the actual sorting needs to be implemented in the model's sort() method.
Use QSortFilterProxyModel like this:
QSortFilterProxyModel* pProxyModel = new QSortFilterProxyModel(pView);
pProxyModel->setSourceModel(pModel);
pView->setModel(pMdl);
pView->setSortingEnabled(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With