Qt: having selected items appear on top in QGraphicsScene

Silnik picture Silnik · Jun 15, 2014 · Viewed 9.4k times · Source

So i have a QGraphicsScene with various items. Some of the can take the same coordinates in the scene. When I display the scene the ones displayed on top are the ones which were added last. Is there a way to make a certain item always appear on top or even better make it reappear on top during the program (rather than deleting it and adding it again)?. I've been thinking about drawing everything beforehand and then using setVisible() to show only the items I want to, but since I want to add new things online it seems to be a bit problematic.

Answer

Nejat picture Nejat · Jun 15, 2014

You can use QGraphicsItem::setZValue ( qreal z ) to set the Z-value of the item. The Z value decides the stacking order of sibling items. An item with a higher Z value will always be drawn on top of another item with a lower Z value. The default Z value is zero. So you can set it to a higher value to bring it on top :

item->setZValue(1);

or bring it to bottom of other items :

item->setZValue(-1);