【发布时间】:2018-10-09 17:52:42
【问题描述】:
我有两个图形项目,RectItem 和 CurveItem。我需要准确检测这两个项目之间的碰撞。我为展位项目实施了shape() 方法。我验证当我从场景self.collidingItems(self.rect_item, mode=Qt.IntersectsItemShape 中移动其中一个项目时是否检测到碰撞。
我通过在场景中移动它们来碰撞项目,但仅在CurveItem 的顶部检测到碰撞。我不明白我在哪里做错了。
代码:
class RectItem(QGraphicsRectItem):
def __init__(self, x, y, w, h):
super().__init__(x, y, w, h)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setPen(QPen(Qt.cyan))
def shape(self):
path = QPainterPath()
path.addRect(self.boundingRect())
return path
class CurveItem(QGraphicsItem):
def __init__(self):
super().__init__()
self.path = self._setupPath()
def paint(self, painter, styles, widget=None):
painter.drawPath(self.path)
def boundingRect(self):
return QRectF()
def shape(self):
return self.path
def _setupPath(self):
path = QPainterPath()
path.moveTo(0, 0)
path.cubicTo(99, 0, 50, 50, 99, 99)
path.cubicTo(0, 99, 50, 50, 0, 0)
return path
class Scene(QGraphicsScene):
def __init__(self):
super().__init__()
self.curve_item = CurveItem()
self.rect_item = RectItem(-50, -50, 25, 25)
self.addItem(self.curve_item)
self.addItem(self.rect_item)
def mouseMoveEvent(self, e):
print(self.collidingItems(self.rect_item, mode=Qt.IntersectsItemShape))
super().mouseMoveEvent(e)
【问题讨论】:
标签: python pyqt pyqt4 pyqt5 qgraphicsitem