Making a QGraphicsRectItem "transparent" to mouse clicks



  • Hi!

    I'm developing an application where lots of points are show on the screen. These points are clickable, and when this happens, lines connecting these points to other ones ("edges") are shown.

    The user may select (using a rubber band) a rectangular section and start some background process. Once it is over, this area is "special" because it has already been processed. Since I want the user to be aware of what are the areas that have been taken care of, I draw QGraphicsRectItem covering the area that was selected with the rubber band.

    Here come the problems.

    I insert the QGraphicsRectItem using the same z value as the points. The problem is, therefore, that since there are so many (so many!!!) points in the scene, the rectangle is almost invisible, since it is somehow covered by the point items.

    If I use a z value for the rectangles greater than that for the points, then the rectangles are perfectly visible, but... since now they "cover" the points, the clicks over these are somehow captured by the rectangles, so I miss the point selection ability.

    I hope that my problem is clear now. The question, then, is very simple: how can I make the rectangles "transparent" to mouse clicks while using a z value that make them appear nicely drawn on the screen?

    Using QT 5.9. Windows.

    Thanks!

    Edit: I tried calling setAcceptedMouseButtons(0) when creating the rectangle, but this didn't worked.

    Edit 2: I've also overriden the mousePressEvent and mouseReleaseEvent methods. These only ignore the event:

    void
    VEProcessedAreaItem::
    mousePressEvent
    (QGraphicsSceneMouseEvent *event)
    {
      {
        event->ignore();
      }
    }
    
    void
    VEProcessedAreaItem::
    mouseReleaseEvent
    (QGraphicsSceneMouseEvent *event)
    {
      {
        event->ignore();
      }
    }
    

    It didn't worked.


  • Lifetime Qt Champion

    Hi,

    You may find an interesting example in Qt'ssetAcceptedMouseButtons.



  • Hi.

    Your answer confirms that this is the right way (using setAcceptedMouseButtons).

    Since I'm already using this method but it does not work, I guess there's something wrong in my code. I'll keep trying.

    Thank you.



  • From the QGraphicsItem docs:

    "The mouse press event decides which item should become the mouse grabber (see QGraphicsScene::mouseGrabberItem()). If you do not reimplement this function, the press event will propagate to any topmost item beneath this item, and no other mouse events will be delivered to this item."

    So there is something fishy going on for sure...

    EDIT: Also, I use items which cover other items all the time. I don't typically have issues what items "catch" the events, unless I have event handlers in there.

    However, consider that certain GraphicsItemFlags can implicitly cause items to accept mouse events (e.g. if the item is moveable, it needs to handle mouse clicks).


Log in to reply