Hello,

Im fairly new to the GraphicsScene and GraphicsView in PyQt5, im trying to wrap my head around how to use it efficiently, and i have just bumped into an issue.

I am looking for a way to change the default mousebutton that moves the items in the GraphicsView.

This is the way i would like the graphicsScene UI to work :

LeftMouse button click on Item = ItemClick signal (like a regular button widget would do)
RightMouse button click on Item = ItemLeftClick signal (open a context menu with a few options specific to the item)

RightMouse button click on Empty space = Open's context menu for GraphicsView (so far so good).

#here
LeftMouse button click on Empty space and drag select rubberband = This Can stay as currently is, clears selection on click then it selects any of the items inside the rubberband rectangle on mouse click release.

MiddleMouse button drag = If items selected, i want this button to be moving my items position.
so far im kinda unable to do this..

this is a snippet of my current mouse logic inside the QtWidgets.QGraphicsView class , im not sure what i would need to do to replace the default leftMouse drag behaviour.

Qt Code:
  1. def mousePressEvent(self, event):
  2. if event.button() == QtCore.Qt.MiddleButton and event.modifiers() == QtCore.Qt.AltModifier:
  3. self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
  4. self.drag = True
  5. self.prev_pos = event.pos()
  6. self.setCursor(QtCore.Qt.SizeAllCursor)
  7. elif event.button() == QtCore.Qt.LeftButton:
  8. self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
  9.  
  10. # This does not work as i wanted, it is only changing the mouse icon
  11. elif event.button() == QtCore.Qt.MiddleButton:
  12. # set hand icon..
  13. self.setDragMode(QtWidgets.QGraphicsView.DragMode.ScrollHandDrag)
  14. # print selected item position in scene.
  15. for x in self.scene().selectedItems() :
  16. print( x.scenePos() )
  17.  
  18. super(View, self).mousePressEvent(event)
  19.  
  20. def mouseMoveEvent(self, event):
  21. if self.drag:
  22. new_scale = self.matrix().m11()
  23. delta = (self.mapToScene(event.pos()) - self.mapToScene(self.prev_pos)) * -1.0 * new_scale
  24. center = QtCore.QPoint(self.viewport().width() / 2 + delta.x(), self.viewport().height() / 2 + delta.y())
  25. new_center = self.mapToScene(center)
  26. self.centerOn(new_center)
  27. self.prev_pos = event.pos()
  28. return
  29. super(View, self).mouseMoveEvent(event)
  30.  
  31. def mouseReleaseEvent(self, event):
  32. if self.drag:
  33. self.drag = False
  34. self.setCursor(QtCore.Qt.ArrowCursor)
  35. super(View, self).mouseReleaseEvent(event)
  36.  
  37. if event.button() == QtCore.Qt.RightButton:
  38. # Context Menu
  39. menu = QtWidgets.QMenu()
  40. save = menu.addAction('save')
  41. load = menu.addAction('load')
  42. selectedAction = menu.exec_(event.globalPos())
  43.  
  44. if selectedAction == save:
  45. common.scene_save(self)
  46. if selectedAction == load:
  47. common.scene_load(self)
To copy to clipboard, switch view to plain text mode