0

我需要帮助,我想添加另一个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()))
4

1 回答 1

0

检查QGraphicsScene是否已单击任何图形元素。如果self.scene().focusItem()返回None,则不选择任何元素。

class QGraphicsViewClass(QGraphicsView):

    def __init__(self):
        super().__init__() 
        self.select_posX = 0
        self.select_posY = 0

    def contextMenuEventmouseDoubleClickEvent(self, event: QtGuiQtWidgets.QContextMenuEventQGraphicsSceneMouseEvent) -> None:
        if(self.scene().focusItem() == None):
            mousePoint = QtCore.QPointF(self.mapToScene(event.pos()))
            self.select_posX = mousePoint.x()
            self.select_posY = mousePoint.y()

            add_new_slot_on_position = QtWidgets.QAction("Add new slot here")
            add_new_slot_on_position.triggered.connect(lambda: self.add_new_slot_on_position())
            add_new_slot_on_position.setIcon(QtGui.QIcon("resources/icons/add_slot.png"))
            
            menu = QtWidgets.QMenu()
            menu.addAction(add_new_slot_on_position)
            menu.exec_(event.globalPos())
        else:
            print("Context menu is disable. Your clicked on graphics item.")

    def add_new_slot_on_position(self):
        print("Add new graphics item on position", self.select_posX, self.select_posY)
于 2022-02-03T19:03:05.483 回答