PDA

View Full Version : QGraphicsScene sorting selected items



akiross
2nd April 2011, 17:14
Hello,
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
~Aki

SixDegrees
2nd April 2011, 19: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.