How do I prevent a Right mouse press event from cancelling item selection in QGraphicsScene?



  • Currently, this code has no effect:

        def mousePressEvent(self, event):
            if self._place is None:
                if event.button() != Qt.RightButton:   # reserved for context menu so it doesn't disturb current selection
                    super().mousePressEvent(event)
            else:
                if event.button() == Qt.LeftButton:
                    self._place = None
                elif event.button() == Qt.RightButton:
                    if self.rightClickOnPlaceHandler is not None:
                        self.rightClickOnPlaceHandler(self._place, event)
    

    in terms of fixing my problem. The problem is that the selected items become no longer selected upon any press of the scene, except of course the focused item remains selected when you press on it. I need the items to remain selected upon right mouse clicking so that the context menu can operate on selected items ("Lock", "Unlock", "Copy", "Delete" [selected]).



  • Solved it. Put this in itemChange handler of your GraphicsItem base class (you need to have made one):

            elif change == self.ItemSelectedChange and self.scene():
                if self.scene().rightMousePressed():
                    return self.isSelected()
                return value
            else:
    

    And in graphics_scene.py:

        def mousePressEvent(self, event):
            if self._place is None:
                if event.button() != Qt.RightButton:   # reserved for context menu so it doesn't disturb current selection
                    super().mousePressEvent(event)
                else:
                    self._rightPress = True  
    #          ....
                        
        def mouseReleaseEvent(self, event):
            self._rightPress = False
            super().mouseReleaseEvent(event)
    

    This works for my app.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.