谁能帮助我如何防止QGraphicsRectItem
元素重叠?
所以我想做当我们移动一个元素并将它放在某个地方以将其返回到我们开始移动的起始位置时,如果已经有一个元素在设置位置。
我想阻止这种重叠。
这就是我的QGraphicsRectItem
班级的样子。
class Slot(QGraphicsRectItem):
def __init__(self, iface, slot_id, positionX, positionY, width, height):
super().__init__()
self.iface = iface #MainWindow
self.id = slot_id
self.positionX = positionX
self.positionY = positionY
self.width = float(width)
self.height = float(height)
self.setPos(self.positionX, self.positionY)
self.setRect(0, 0, self.width, self.height)
self.blackPen = QPen(Qt.black)
self.blackPen.setWidth(1)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
def mouseReleaseEvent(self, mouseEvent):
"""
Executed when the mouse is released from the item.
"""
super().mouseReleaseEvent(mouseEvent)
self.update()
def shape(self):
"""
Returns the shape of this item as a QPainterPath in local coordinates.
"""
path = QPainterPath()
path.addRect(self.rect())
return path
def boundingRect(self):
"""
Returns the bounding rect of the shape (including the resize handles).
"""
o = self.handleSize + self.handleSpace
return self.rect().adjusted(-o, -o, o, o)
def paint(self, painter, option, widget=None):
"""
Paint the node in the graphic view.
"""
painter.setBrush(QBrush(QtGui.QColor("#F9F9F9")))
painter.setPen(self.blackPen)
painter.drawRect(self.rect())
painter.setRenderHint(QPainter.Antialiasing)
for handle, rect in self.handles.items():
if self.handleSelected is None or handle == self.handleSelected:
painter.drawEllipse(rect)