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.
These are is viewed through a QGraphicsView. Adding an item is simple, you can just call the QGraphicsScene function addItem, which takes a QGraphicsItem*. In this case, you have a pointer to the item, so if you call QGraphicsScene::removeItem, it will remove it from the scene and you can then delete the item.
QGraphicsView visualizes the contents of a QGraphicsScene in a scrollable viewport. To create a scene with geometrical items, see QGraphicsScene's documentation. QGraphicsView is part of the Graphics View Framework.
Graphics View provides a surface for managing and interacting with a large number of custom-made 2D graphical items, and a view widget for visualizing the items, with support for zooming and rotation.
QGraphicsScene is part of the Graphics View Framework. QGraphicsScene also provides functionality that lets you efficiently determine both the location of items, and for determining what items are visible within an arbitrary area on the scene.
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);
After doing some research I think that @Leslie 's answer is in a way more correct than @Nejat 's. However it is very inefficient if you have a lot of items and you have to do that check very often.
I my case I have a QGraphicsTextItem
that shows the coordinates of the cursor inside my scene.
void QCVN_SceneView::mouseMoveEvent(QMouseEvent *event)
{
QPointF cursorPoint = mapToScene(event->pos());
// cout << "Event pos: x=" << event->pos().x() << ", y=" << event->pos().y() << endl;
// cout << "Event pos (in scene): x=" << cursorPoint.x() << ", y=" << cursorPoint.y() << endl;
QString coords = QString("%1, %2")
.arg(cursorPoint.x())
.arg(cursorPoint.y());
cursorSceneCoords->setPlainText(coords);
cursorSceneCoords->setPos(cursorPoint);
QGraphicsView::mouseMoveEvent(event);
}
Obviously if I have 10000000 items in the scene using setZValue(1)
will not do the trick unless all items are with a z-value
under 1. Iterating through the whole list of items is also a bad way of doing it.
I gave it a little thought and it struck me once I remembered that setZValue()
uses a qreal
argument. Depending on the system it can be a double
or a float
however this is not important. We can simply use std::numeric_limits
to get the very, very top value possible. Of course we have to limit all our items in the scene (except the desired once that we want on top) to have a maximum z-value
of maximum possible value minus 1 (or 0.1 or whatever). So if you do
YOUR_GRAPHICS_ITEM.setZValue(std::numeric_limits<qreal>::max());
the item will always be on top of the rest.
This question is a little confusing. The subject mentions wanting items on top when selected, while the description talks about more permanent ordering concerns (creation and such). I'm adding this answer to clarify possible solutions for those coming here due to the subject:
zValue
can be set to -100.zDepth
.
So, when items are selected you could run through all items and adjust the order and you don't have to worry about trying to reset zDepth
back to what it was before when done (I suppose you could still want to restore, but at least this way the concerns are separate from zDepth
). Note, FWIW, I find the stackBefore
ordering of sibling
to feel backwards.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With