Skip to content Skip to sidebar Skip to footer

Pyqt. How To Block Clear Selection On Mouse Right Click?

I have a QGraphicsScene and many selectable items. But when I click the right mouse button - deselects all objects. I want show menu and edit selected objects but have automatic de

Solution 1:

Daniele Pantaleone answer gave me an idea and I have modified the function of mousePressEvent() and immediately got the desired effect me

def mousePressEvent(self, event):
    ifevent.button() == Qt.MidButton:
        self.__prevMousePos = event.pos()
    elif event.button() == Qt.RightButton: # <--- add this 
        print('right')
    else:
        super(MyView, self).mousePressEvent(event)

Solution 2:

A possible solution would be to use mouseReleaseEvent to display the contextual menu instead of contextMenuEvent:

defmouseReleaseEvent(self, mouseEvent):
    if mouseEvent.button() == Qt.RightButton:
        # here you do not call super hence the selection won't be cleared
        menu = QMenu()
        menu.exec_(mouseEvent.screenPos())
    else:
        super().mouseReleaseEvent(mouseEvent)

I haven't been able to test it but I guess it should work. The point is that the selection is cleared by default by QGraphicsScene, so what you need to do is to prevent the clearing from happening when certain conditions are met, in your case when the contextual menu needs to be displayed.

Post a Comment for "Pyqt. How To Block Clear Selection On Mouse Right Click?"