Have QGraphicsItem receive ALL mouse moves?

Question:

I have a subclass of QGraphicsItem. By default, QGraphicsItem only receives mouseMoveEvents if the pointer is within the boundingRect of the item AND a mousePressEvent has also happened.

I want this subclass to receive ALL mouseMoveEvents that occur within the QGraphicsScene that it’s a member of (preferably in item coordinates). Is this possible?

Asked By: Daniel

||

Answers:

Yes. You can use QGraphicsItem::grabMouse() to ensure that you get all mouse events. If you do so, then Qt promises the following:

This item will receive all mouse events for the scene until any of the
following events occurs:

  • The item becomes invisible
  • The item is removed from the scene
  • The item is deleted
  • The item call ungrabMouse()
  • Another item calls grabMouse(); the item will regain the mouse grab when the other item calls ungrabMouse().
Answered By: cgmb
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.