I am adding items on a QGraphicsScene
. Once added, items may be further processed.
I would like to skip the items completely obscured by other items. It seems that there are several QGraphicsItems
dedicated to this task.
Given two QGraphicsItem
objects, in the shape of a rectangle, identical in size, only differing by color, and each with identical boundingRect()
, each at exactly the same pos()
, but with different zValue()
:
for(int i = 0; i < scene.items().size(); ++i)
{
if(scene.items().at(i)->isObscured())
{
continue;
}
// do work
}
I also tried
if(scene->items().at(i)->isObscured(scene->items().at(i)->boundingRect()))
Neither option skips the item that is not visible.
If I increase slightly the size of the rectangle on top, the isObscured
function works (either version). But with identical shapes, one on top of the other, logically the bottom one would still be obscured...
Would a reasonable fix be to add 1 pixel on each side of the rectangle ? would that not lead to weird situations ?
How can I make items that are covered be skipped ?
This issue is even more complicated if one item is covered by several other items, neither of them fully covering the item.
If I increase slightly the size of the rectangle on top, the isObscured function works (either version). But with identical shapes, one on top of the other, logically the bottom one would still be obscured...
In the documentation for QGraphicsItem::isObscuredBy(QGraphicsItem* item), it states: -
The base implementation maps item's opaqueArea() to this item's coordinate system, and then checks if this item's boundingRect() is fully contained within the mapped shape.
Looking at opaque area and the mapped shape, this is related to the opaqueArea function which states: -
An area is opaque if it is filled using an opaque brush or color (i.e., not transparent).
With two identical items, it's not the area of the bounding rectangles that are compared, but the opaqueArea of the QGraphicsItem with the boundingRect of the other item.
If your two items are failing when they are the same, on top of each other but with different colour, then the returned opaqueArea for the first item is not matching the bounding rect of the second.