View Full Version : QGraphicsScene sorting selected items

2nd April 2011, 18:14
from documentation

QList<QGraphicsItem *> QGraphicsScene::selectedItems () const
Returns a list of all currently selected items. The items are returned in no particular order.

but I need to track the selection order if user selects items individually (or any order if uses rubber band). What shall I change to achieve this behavior?
I was thinking to reimplement mousePressEvent() and track of clicked items, but since QGraphicsScene already handles selections, I prefer to do as less damage as possible to the current behavior :)

Any help is welcome
Thanks in advance

2nd April 2011, 20:23
Make user of QGraphicsItems.itemChange(QGraphicsItem::ItemSelec tedHasChanged); this gets called when, for instance, the selection state changes. You can then call a static function that provides incrementing values each time it is called to set internally. Finally, you can sort your items on this value.

There are a few wrinkles involved, like keeping track of items being selected multiple times, or unselected, but nothing terribly complicated.