我需要帮助,我想添加另一个QMenu
,但只有在我在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)
我的 QGraphicsViewClass:
class QGraphicsViewClass(QGraphicsView):
def __init__(self):
super().__init__()
def mouseDoubleClickEvent(self, event: QtWidgets.QGraphicsSceneMouseEvent) -> None:
mousePoint = QtCore.QPointF(self.mapToScene(event.pos()))