2

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 deselect any time when right click at mouse...

Perhaps the problem is that I have included an rubber selection. selection of objects in the end is how the right and the left mouse button when I pull the frame and therefore is reset at single time you press the right button...

How to leave objects highlighted when you click on the right mouse button? Or it may be necessary to disable the rubber selection of the right button?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Massimo
  • 836
  • 3
  • 17
  • 38

2 Answers2

2

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):
    if event.button() == Qt.MidButton:
        self.__prevMousePos = event.pos()
    elif event.button() == Qt.RightButton: # <--- add this 
        print('right')
    else:
        super(MyView, self).mousePressEvent(event)
Massimo
  • 836
  • 3
  • 17
  • 38
1

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

def mouseReleaseEvent(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.

Daniele Pantaleone
  • 2,657
  • 22
  • 33
  • no. You misunderstood. menu by pressing the right mouse button appears. but the selection of objects removed. Now I press the right button and deselected first, and then the menu appears. plus to it by pressing the right button and hold, you can also select objects. I also need to allocation did not work at all or remove the selection by pressing the right mouse button. – Massimo Mar 18 '16 at 07:05