1

When the user selects QGraphicsItems, I want to be able to keep track of the order of how they are selected. Right now I'm using QGraphicsScene.selectedItems(), but it doesn't give me what was the last selected object.

This seems clear after checking the documentation for selectedItems(): https://srinikom.github.io/pyside-docs/PySide/QtGui/QGraphicsScene.html#PySide.QtGui.PySide.QtGui.QGraphicsScene.selectedItems

Returns a list of all currently selected items. The items are returned in no particular order.

So is this something I need to keep track of myself, or am I missing another way to do this?

Thank you!

Update:

This seems to work, but may not be the most elegant approach, especially with lots of items. Let me know if I can improve it!

def selectionChanged(self):
    if self.scene().selectedItems():
        # Remove any item that was de-selected
        for i in range(len(self.sortedItems)-1, -1, -1):
            if not self.sortedItems[i].isSelected():
                self.sortedItems.pop(i)
        # Add new items
        for item in self.scene().selectedItems():
            if item not in self.sortedItems:
                self.sortedItems.append(item)
    else:
        del self.sortedItems[:]
Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • Seems a reasonable solution... And you are correct about `selectedItems()` not being ordered the way you want. Generally the order matches the order in `items()` which is the order in which items are added to the scene. For a different problem where I needed a sorted items list, I created a map with the `value` being the index in `items()`. – Thalia Sep 01 '15 at 16:25

0 Answers0