Draw order of QGraphicsItems
-
I would like to understand and solve one problem that involve "uncle/cousin" relationship between items. I Have a button class and two instances of it, say it A and B. The button class inherits from QGraphicsItem and has a method that is QGraphicsRectItem, I use it to represent a tooltip.
The problem is: when I draw the buttons A and B next to the other, the tooltip of A is drawn behind B, but I want that any tooltip is always drawn on top of any object of button class.
-
@JonB Just to give a follow up: I could solve the problem by not parenting the attribute as you suggested, so both became toplevel. Based on that, I only had to guarantee that the tooltip object would have a zValue always greater than any button zValue
-
@leonardoMB
https://doc.qt.io/qt-5/qgraphicsitem.html#paintingItems are painted by the view, starting with the parent items and then drawing children, in ascending stacking order. You can set an item's stacking order by calling setZValue(), and test it by calling zValue(), where items with low z-values are painted before items with high z-values. Stacking order applies to sibling items; parents are always drawn before their children.
and has a method that is QGraphicsRectItem, I use it to represent a tooltip
-
Hi @JonB. The second tip may help, I didn't pay attention to this tooltip method
About the painting order, I have read the Qt text, but didn't find a way to solve the case Above, I mean, I could work with the zValues, but I want something more robust, but not so complicated. In a case where I would have 1000 buttons in any order, for example, I still would want the tooltip to be drawn on top of every button -
@leonardoMB said in Draw order of QGraphicsItems:
Above, I mean, I could work with the zValues, but I want something more robust, but not so complicated. In a case where I would have 1000 buttons in any order, for example, I still would want the tooltip to be drawn on top of every button
What is not robust, or complicated, about
toolTipItem.setZValue(VERY_LARGE_NUMBER)
?To confirm: one single tooltip item to be drawn up-front no matter how many buttons/other items, right, not one tooltip per item?
-
@JonB one tooltip per button, so "n" buttons have "n" tooltips. Each button with its own tooltip.
toolTipItem.setZValue(VERY_LARGE_NUMBER)
But How can I use Zvalue? the documentation says it must be siblings, but the tooltip is "cousin" of the others buttons
-
@leonardoMB said in Draw order of QGraphicsItems:
one tooltip per button, so "n" buttons have "n" tooltips. Each button with its own tooltip.
That changes everything. It was not what I understood, I do now.
Are these "tooltip windows" to be present (visible) all the time their corresponding item is visible? They are permanently present & visible, e.g. not like a tooltip which only appears when you hover over an item, and not only one item in the whole view displays its tooltip at a time?
-
@leonardoMB
OK, we are back to: at any one instant, only one tooltip is visible on the view.Originally you wrote:
The button class inherits from QGraphicsItem and has a method that is QGraphicsRectItem, I use it to represent a tooltip.
You didn't mean a method, you turn out to mean a child, right? That's vitally important.
So you have you tooltip rect items as children of your gfx items. Then you want button A to be behind button B, but button A's tooltip to be in front of button B, right?
Not certain, but I don't think parent/child allows this. I suspect A's child tooltip will always be directly on top of A, wherever that is the z-order. Though you could still try with
toolTipItem.setZValue(VERY_LARGE_NUMBER)
to see.You could try not parent-child but QGraphicsItemGroup with separate z-orders. But again I think it won't do what you want.
Instead of parent/child gfx items, you could create a non-parented tooltip rectangle item with a high z-order value and using the rectangle's coordinates to place it at the instant you want to show it. That would place it at the top of all other items.
Finally if that
QGraphicsItem::setToolTip()
can be used to do what you want to achieve with the z-order it would be a lot simpler! -
I did mean attribute, but I misswritten, so you are right: I mean a child. Exactly:
I want button A to be behind button B, but button A's tooltip to be in front of button B.
I will try some of your ideas, unfortunatelyQGraphicsItem::setToolTip()
cannot be implemented right now. Thanks in advance -
@JonB Just to give a follow up: I could solve the problem by not parenting the attribute as you suggested, so both became toplevel. Based on that, I only had to guarantee that the tooltip object would have a zValue always greater than any button zValue
-
@leonardoMB
Indeed exactly as I suggested might be the simplest possibility. The only thing is that you have manage showing it at the correct place relative to your rectangle.It is interesting that you have ended up accepting to use z-value to achieve on-top. When you said originally
I could work with the zValues, but I want something more robust, but not so complicated.
I take it you decided z-value is not flaky/complicated after all :)