How to setSortRole for QTreeWidget ?



  • I have QTreeWidget based class and need to do sorting by Qt::UserRole. How can I do this ? I know that QStandartItemModel has such method and treeWidget has model() method which return model, but it's QAbstractItemModel type which has no setSortRole method.



  • QSortFilterProxyModel seems the obvious choice. It supports a sortRole property you can use to set the role you want to sort on.



  • Yes, but I use QTreeWidget, non view and setModel is private method in QTreeWidget. So I can't do this: @QSortFilterProxyModel sortModel;
    sortModel.setSourceModel(model());
    setModel(&sortModel);@



  • Ah, yes, right.
    I'd suggest to move away from QTreeWidget towards using QTreeView then :-) The API for QStandardItemModel is quite similar to the API of QTreeWidget for items, so the changes to your code should be minimal.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.