Auto sort when pasting in QTreeWidget

I have a QTreeWidget in which I insert elements, and the user can select a column to sort it. When elements are inserted, they are simply added to the end, and not automatically sorted. If I click the title to switch between up / down, it sorts the current items.

I decided that I could call sortItems () and use the column that is returned from sortColumn (), but I cannot see how I can see if the user is sorting in ascending or descending order.

I am not worried about the effectiveness of this, so I do not want to wait until the inserts are completed, and then sort. I would like a sorted list in real time.

Thanks!

+3
source share
2 answers

If your tree widget is called treeWidget, you can call the header () method, which is from QTreeView QTreeWidget, then sortIndicatorOrder () from the QHeaderView class:

treeWidget->header()->sortIndicatorOrder()

In this case, you know the current sort order of the user, and you can apply your sort to the insert in accordance with this.

+2
source

I don’t have any settings for testing, but according to the documentation, this should lead to the fact that sorting will be performed as elements are added.

...
treeWidget.sortByColumn(0, Qt::AscendingOrder); // column/order to sort by
treeWidget.setSortingEnabled(true);             // should cause sort on add

Qt recommends not to do this, as the cost will be produced and say that you should set up the sort after all the additions are complete. Hope this helps.

+2

Source: https://habr.com/ru/post/1707088/


All Articles