【问题标题】:Restrict movement area of QGraphicsItem inside of polygon area限制 QGraphicsItem 在多边形区域内的移动区域
【发布时间】:2020-07-07 12:26:32
【问题描述】:
我试图限制 QGraphicsItem 在继承自 QGraphicsPathItem 的父对象内的移动,该对象具有任意复杂形状(不是正方形)。
据我了解,我应该提取移动对象的每个点并检查它们中的每一个是否不包含在父 QPainterPath 中。然后将父 QPainterPath 分割成小的矩形多边形,并在每个子多边形的边界框内限制移动区域。
所以,我想知道是否还有其他选择。谢谢。
【问题讨论】:
标签:
c++
qt
qgraphicsscene
qgraphicsitem
【解决方案1】:
如果 QGraphicsItem 移动事件仍在 QGraphicsPathItem 内,也许您可以检查它。
检查本身,你可以用这个QGraphicsItem method:
bool QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
为了确保该项目仍完全包含在其父项中,请将 mode 设置为 Qt::ContainsItemShape
如果为false,则将QGraphicsItem返回到之前的位置
编辑:如果 QGraphicsItems shape 在其他 QGraphicsItems shape 内而不是其边界框内,则此检查返回。