Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I prevent a Right mouse press event from cancelling item selection in QGraphicsScene?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 897 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by enjoysmath
    #1

    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]).

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0
    • enjoysmathE Offline
      enjoysmathE Offline
      enjoysmath
      wrote on last edited by enjoysmath
      #2

      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.

      https://github.com/enjoysmath
      https://math.stackexchange.com/users/26327/exercisingmathematician

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved