Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event filter on QGraphicsItem

Tags:

c++

events

qt

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?

like image 547
eyecreate Avatar asked Dec 08 '22 02:12

eyecreate


1 Answers

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.

like image 91
Frank Osterfeld Avatar answered Dec 09 '22 15:12

Frank Osterfeld