将QGraphicsItem的移动区域限制在多边形区域内

Restrict movement area of QGraphicsItem inside of polygon area

本文关键字:区域 多边形 移动 QGraphicsItem      更新时间:2023-10-16

我试图限制QGraphicsItem在从具有任意复杂形状(而非正方形(的QGraphicsPathItem继承的父对象内部的移动。

据我所知,我应该提取移动对象的每个点,并检查它们中的每个点是否都不包含在父QPainterPath中。然后将父QPainterPath拆分为小的矩形多边形,并限制每个子多边形的边界框中的移动区域。

所以,我想知道是否还有其他选择。谢谢

如果QGraphicsPathItem仍在QGraphicsPathItem中,则可以检查QGraphicsItem移动事件。

检查本身,您可以使用以下QGraphicsItem方法:

bool QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

为了确保该项仍然完全封闭在其父项内,请将模式设置为Qt::ContainsItemShape

如果为false,则将QGraphicsItem返回到其先前位置

EDIT:如果QGraphicsItems形状在其他QGraphicsIitems形状内,而不是在其边界框内,则此检查返回。