Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Items are not sorted in a tree view in Qt

Tags:

c++

qt

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?

like image 364
user1899020 Avatar asked Jan 24 '26 00:01

user1899020


1 Answers

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);
like image 116
spiritwolfform Avatar answered Jan 26 '26 16:01

spiritwolfform