Search code examples
pythonpyqtselectionqtreewidget

Native PyQt function returning selected QTreeWidgetItems in order of selection?


I have a QTreeWidget which uses the QAbstractItemView.ExtendedSelection property, allowing users to shift click and control click items.

I'd like to preform actions on these items based on the order in which they are selected-- I can think of ways to design my own systems to determine the selection order, but is there a native function/property that will return a list of items sorted via selection order?

Thank you for your advice!


Solution

  • The underlying selection model naturally preserves in the order in which items are selected. So you don't really need to do anything special - just iterate over them like this:

    for item in treewidget.selectedItems():
        print(item.text(0))