0

I am trying to avoid wasting time doing something if my lack of knowledge of QT could avoid me even to try.

Supposed a have a class X not derived from QGraphicsItems with fields containing among others several QGraphicsItems. Can I define event filters in X and install them on the QGraphicsItems in order to let X receive the event before the QGraphicsItems themselves? Thanks.

Francesco
  • 481
  • 2
  • 4
  • 16
  • In function `QObject::installEventFilter(QObject *)` both argument and class should be `QObject`s, but `QGraphicsItem` is not a QObject. I don't know what you X class is. Therefore, I think it is not possible. – vahancho Apr 01 '14 at 12:09
  • Sure, but you want to install the event filter on the object that actually receives the events. In Qt, such objects must derive from `QObject`. Thus you need to filter events reaching `QGraphicsScene`. – Kuba hasn't forgotten Monica Apr 01 '14 at 15:40

1 Answers1

0

If your design allows, rather than objects inheriting from QGraphicsItem, inherit from QGraphicsObject, which will allow you to use the standard QObject::installEventFilter.

Otherwise, you would need to have class 'X' inherit from QGraphicsItem.

Then, you can install an event filter from one GraphicsItem to another through the QGraphicsScene. This is detailed in the Qt Documentation here.

To filter another item's events, install this item as an event filter for the other item.

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's events are filtered by ellipse's sceneEventFilter() function.

 ellipse->installSceneEventFilter(line);
 // ellipse's events are filtered by line's sceneEventFilter() function.
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Thanks. It makes my life more complicated. I am basically needed a class with QGraphicsItems and few non QT parameters, where the main QGraphicsItem can hide or shoe the rest when the mouse hovers on it. – Francesco Apr 01 '14 at 12:30