Results 1 to 3 of 3

Thread: QGraphicsView, change mouse click/move item button (left is default)

  1. #1
    Join Date
    Dec 2020
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Question QGraphicsView, change mouse click/move item button (left is default)


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

    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)
    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() )
    18. super(View, self).mousePressEvent(event)
    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)
    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)
    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())
    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 

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 864 Times in 851 Posts
    Qt products

    Default Re: QGraphicsView, change mouse click/move item button (left is default)

    I am not a Python or PyQt expert, but based on my C++ experience, it you want to override the mouse behavior completely, you do not want to call the superclass handler. If you only want to partially override it, you handle those parts and only call the superclass method for the things you want to stay as-is. And if you want to be sure the event is "eaten" and not passed anywhere else, you return "true" from the event handler.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    sQaT (10th December 2020)

  4. #3
    Join Date
    Dec 2020
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default Re: QGraphicsView, change mouse click/move item button (left is default)

    Holy smoke i did it!
    Thank you so much for the pointers mate, you where right .

    The way i managed to do it is inside my QGraphicsView Class, i am handling the mousePress event and MouseMove event:
    ill probably have to also override the default rubberband selection of the items and manually handle that, since i want to avoid the left click selection
    in favour of left click on item is "click" .

    i had to also turn off the default Class flag inside my QGraphicsItem:
    #self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovabl e)

    This way, i simply handle the positions manually.

    , i still have a few things to fix, which im not quite sure im doing correctly, but this seems to work closer to the way i wanted.

Similar Threads

  1. Replies: 1
    Last Post: 18th August 2016, 15:29
  2. Mouse Right or Left click ???
    By anjanu in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2011, 19:41
  3. Replies: 4
    Last Post: 29th August 2010, 19:16
  4. Qtablewidget left mouse click event in python
    By gerocampo in forum Newbie
    Replies: 0
    Last Post: 23rd July 2010, 18:05
  5. Emulating Enter key with left mouse click
    By wconstan in forum Newbie
    Replies: 6
    Last Post: 30th December 2009, 17:16

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.