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[:]