Is it possible to have an event filter on a QGraphicsItem
? Eventfilter has a param that gives you a QObject
, but since QGraphicsItem
isn't derived from QObject
, then how would it work?
QGraphicsItem's are not QObjects, but they still receive events, managed by their QGraphicsScene. And it also supports event filtering. QGraphicsItem::installSceneEventFilter( QGraphicsItem* filterItem ) installs another item to receive events. Override sceneEventFilter() in the filter item to handle them. It works analoguously to QObject::eventFilter. Important: The item you install the filter on must be already added to a scene to make it work.
If the filter item should do nothing else but filter, i think the easiest way is to derive from QGraphicsItem, implement paint() do no nothing and boundingRect() returning an empty rect. And reimplement sceneEventFilter of course.
Also note that some event classes change in QGraphicsView context, e.g. QMouseEvent becomes QGraphicsSceneMouseEvent, so make sure to filter for the right thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With