tags:

views:

28

answers:

3

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?

A: 

No. You can not install an event filter to QGraphicsItem directly since it's not a QObject. If you are deriving your own QGraphicsItem, inherit from QGraphicsObject so it will gain QObject functions.

One possibility is use http://doc.trolltech.com/latest/qgraphicsitem.html#installSceneEventFilter but it;s more limited since it requires the handler to be a QGraphicsItem too.

Stephen Chu
A: 

The first thing that popped into my mind was this:

Create a new class, derived from both QGraphicsItem and QObject, since these are unrelated (as far as a glance at the docs tells me), you should have what you wanted.

.... But then I looked at the docs more closely and found QGraphicsObject, which is probably exactly what you want, it even already has the member eventFilterlink text

rubenvb
A: 

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.

Frank