Ignore mouse events over transparent parts of svg image in qgraphicsview?

I am working on a graphical representation (using C ++ and Qt) that contains quite a few svg images. I intercept clicks on them, but I would not want to receive events (or to ignore them) when the mouse is over the transparent parts of svg elements.

Is it possible?
Should svg files be specifically designed for this use?
Is there a hidden Qt option that I haven't heard about?

+4
source share
3 answers

Having no choice but to find the difficult answer to my question, here is what I did:

  • searched for the definition of mousePressEvent in QGraphicsSvgItem.cpp. Not found.
  • searched for the definition of mousePressEvent in QGraphicsItem.cpp (ancestor of QGraphicsSvgItem). The method exists, but no corresponding action was found there.
  • searched for mousePressEvent calls in QGraphicsItem.cpp. Found myself reading the code QGraphicsItem :: sceneEvent (), the mouse event dispatcher for the Qt graphics scene. It seems that there are no differences in the different zones of the graphic elements.

Therefore, the sad answer is: Qt does not allow this behavior.

+1
source

There is a CSS property that can be applied to SVG elements, pointer-events , although by default it is visiblePainted :

This element can be a target element for pointer events when the visibility property is set to visible and when the pointer is above the “painted” area. The pointer is above the colored area if it is above the inner part (that is, filled) of the element, and the fill property has an actual value that is different from none, or it is around the perimeter (that is, the stroke) of the element and "The stroke property has a value that is different from value.

Which will mean that the graphical representation of Qt does not support it.

+3
source

To complete other answers:

When re-implementing events, it is important to call the base class event for default cases; if not, the transparency of the event over the unpainted parts is lost.

eg.

 virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent *e) override { if (/* any condition*/) { // Do some specific behaviour } else QGraphicsItem::mouseReleaseEvent(e); } 
0
source

Source: https://habr.com/ru/post/1340142/


All Articles