QGraphicsItem Event Filter

Is it possible to have an event filter on QGraphicsItem? Eventfilter has a parameter that gives you QObject, but since QGraphicsItemit is not derived from QObject, how does it work?

+3
source share
4 answers

Edit: Use QGraphicsItem :: installSceneEventFilter as suggested in @Frank's answer. Example:

QGraphicsScene scene;
QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20));
QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));
line->installSceneEventFilter(ellipse);
// line events are filtered by ellipse sceneEventFilter() function.
ellipse->installSceneEventFilter(line);
// ellipse events are filtered by line sceneEventFilter() function.

The first thing that appeared in my head was the following:

Create a new class, obtained from both QGraphicsItem, and QObject, since they are not interconnected (as far as the documents say), you should have what you wanted.

.... QGraphicsObject, , , , , eventFilter

+3

QGraphicsItem QObjects, , QGraphicsScene. . QGraphicsItem:: installSceneEventFilter (QGraphicsItem * filterItem) . sceneEventFilter() , . QObject:: eventFilter. : , , , .

, , , - QGraphicsItem, paint() , boundingRect() . , , sceneEventFilter.

, QGraphicsView, . QMouseEvent QGraphicsSceneMouseEvent, , .

+10

. QGraphicsItem , QObject. QGraphicsItem, QGraphicsObject, QObject.

- http://doc.trolltech.com/latest/qgraphicsitem.html#installSceneEventFilter, , , QGraphicsItem.

0
source

If you subclass QGraphicsView, you also have the option of simply overriding any of the many functions ...Event(). In such cases, you do not need an event filter.

-1
source

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


All Articles