Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-3689

In graphics view framework, the event filter does not receive events of the child items if setHandlesChildEvents() is true

    XMLWordPrintable

Details

    Description

      Using setHandlesChildEvents(true) on a widget, the widget events handler methods receive the events from the childs items. With an event filter installed on the widget, the filter do not show the same behavior as the event handler, the event received are only the ones from the widget.

      The expected behavior would be to receive the events of the child in the parent filter.

      Here is a test case for this problem:

      #include <QtTest/QtTest>
      #include <QGraphicsItem>
      #include <QGraphicsView>
      #include <QGraphicsWidget>
      #include <QGraphicsScene>

      class Filter : public QGraphicsWidget
      {
      public:
      Filter():eventReceived(false){}
      bool eventReceived;
      protected:
      bool sceneEventFilter ( QGraphicsItem * /watched/, QEvent * /event/ )

      { eventReceived = true; return false; }

      };

      class Test : public QObject{
      Q_OBJECT

      private slots:
      void testEventFilterWithHandlesChildEvents()

      { QGraphicsView view; QGraphicsScene scene; view.setScene(&scene); QGraphicsRectItem *parent = scene.addRect(0, 0, 50, 50, QPen(), Qt::green); parent->setHandlesChildEvents(true); Filter *filter = new Filter; scene.addItem(filter); parent->installSceneEventFilter(filter); QGraphicsRectItem *child = scene.addRect(0, 50, 50, 50, QPen(), Qt::blue); child->setParentItem(parent); view.show(); QPoint point(view.mapFromScene(child->mapToScene(child->rect().center()))); QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, point); view.hide(); QCOMPARE(filter->eventReceived, true); }

      };

      QTEST_MAIN(Test)
      #include "test.moc"

      And here is an example to visualize the problem, click on the green item, the event are shown in debug, click on the blue item, the events are not shown:

      #include <QtGui>

      class Filter : public QGraphicsWidget
      {
      protected:
      bool sceneEventFilter ( QGraphicsItem * watched, QEvent * event )

      { qDebug() << watched << event; return false; }

      };

      int main(int argc, char **argv){
      QApplication app(argc, argv);
      QGraphicsView view;
      QGraphicsScene scene;
      view.setScene(&scene);
      QGraphicsRectItem *parent = scene.addRect(0, 0, 50, 50, QPen(), Qt::green);
      parent->setHandlesChildEvents(true);

      Filter *filter = new Filter;
      scene.addItem(filter);
      parent->installSceneEventFilter(filter);

      QGraphicsRectItem *child = scene.addRect(0, 25, 50, 50, QPen(), Qt::blue);
      child->setParentItem(parent);

      view.show();
      return app.exec();
      }

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            menard Alexis Menard (closed Nokia identity) (Inactive)
            poulain Benjamin Poulain (closed Nokia identity) (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes