如何跳过被相同 QGraphicsItem 遮挡的 QGraphicsItem?

How can I skip QGraphicsItems obscured by identical QGraphicsItems?

我正在 QGraphicsScene 上添加项目。添加后,项目可能会被进一步处理。

我想跳过被其他项目完全遮挡的项目。似乎有几个 QGraphicsItems 专用于此任务。

给定两个 QGraphicsItem 个矩形的对象,大小相同,仅颜色不同,并且每个对象 boundingRect() 完全相同 pos() , 但不同 zValue():

for(int i = 0; i < scene.items().size(); ++i)
{
    if(scene.items().at(i)->isObscured())
    {
        continue;
    }
    // do work
}

我也试过了

    if(scene->items().at(i)->isObscured(scene->items().at(i)->boundingRect()))

这两个选项都不会跳过不可见的项目。

如果我稍微增加顶部矩形的大小,isObscured 函数(任一版本)都有效。但是如果形状相同,一个在另一个上面,逻辑上底部的那个仍然会被遮挡...

在矩形的每一边添加 1 个像素是否合理?这不会导致奇怪的情况吗?

如何跳过覆盖的项目?

如果一个项目被其他几个项目覆盖,而没有一个完全覆盖该项目,这个问题就更复杂了。

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...

QGraphicsItem::isObscuredBy(QGraphicsItem* item) 的文档中指出:-

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.

查看不透明区域和映射形状,这与 opaqueArea 函数相关,该函数声明:-

An area is opaque if it is filled using an opaque brush or color (i.e., not transparent).

对于两个相同的项目,比较的不是边界矩形的面积,而是 QGraphicsItem 的不透明区域与另一个项目的 boundingRect。

如果您的两个项目在相同、彼此重叠但颜色不同时失败,则第一个项目返回的不透明区域与第二个项目的边界矩形不匹配。