Hi,

I've implemented a sub class of QGraphicsView (MyGraphicsView). I want to install an event filter on this view which handles all "user events". But I've got some trouble with the event handling in Qt.

I thought the propagation order is always:

installed filter -> QObject::event()-> event-specific function (like paintEvent)

I've experienced that this is only true for some events like e.g. QPaintEvent and QKeyEvent in case of QMouseEvent or QWheelEvent the propagation order is:

event-specific function -> installed filter -> QObject::event().

So if I would like to filter QMouseEvents or QWheelEvents for my sub class of QGraphicsView I have to reimplement the event-specific functions (e.g. mouseMoveEvent) there I have to ignore() the event so that the installed filter receives the event. If not doing so the event filter doesn't receive these events.

To me this looks a bit strange. The Qt 4.5 Documentation says : "An event filter gets to process events BEFORE the target object does ..."

Is this a bug a feature or is there something wrong with my code?

I've attached a code snippet and the result for QPaintEvent, QMouseEvent, QKeyEvent and QWheelEvent.

Thank you in advance

Markus

PS: I'm using Qt 4.5.1.


Qt Code:
  1. #include "filterevents.h"
  2.  
  3. filterEvents::filterEvents(QWidget *parent)
  4. : QWidget(parent)
  5. {
  6. // set up from with my graphics view
  7. view = new MyGraphicsView();
  8. layout = new QHBoxLayout();
  9. layout->addWidget(view);
  10. setLayout(layout);
  11.  
  12. // install event filter
  13. view->installEventFilter(this);
  14. }
  15.  
  16. bool filterEvents::eventFilter (QObject * object, QEvent * event)
  17. {
  18. if(event->type() == QEvent::KeyPress ||
  19. event->type() == QEvent::Wheel ||
  20. event->type() == QEvent::MouseButtonPress ||
  21. event->type() == QEvent::Paint)
  22. qDebug() << event << "in filter.";
  23.  
  24. return QObject::eventFilter(object, event);
  25. }
  26.  
  27.  
  28.  
  29. #include "MyGraphicsView.h"
  30. #include <QDebug>
  31.  
  32. MyGraphicsView::MyGraphicsView() {
  33. setMouseTracking(true);
  34. }
  35.  
  36. MyGraphicsView::~MyGraphicsView() {
  37. }
  38.  
  39.  
  40. bool MyGraphicsView::event(QEvent * event) {
  41. if(event->type() == QEvent::KeyPress ||
  42. event->type() == QEvent::Wheel ||
  43. event->type() == QEvent::MouseButtonPress ||
  44. event->type() == QEvent::Paint)
  45. qDebug() << event << "in my graphics view (event).";
  46.  
  47. return QGraphicsView::event(event);
  48. }
  49.  
  50.  
  51. void MyGraphicsView::paintEvent(QPaintEvent * event) {
  52. qDebug() << event << "in my graphics view (paintEvent).";
  53. event->ignore();
  54. }
  55.  
  56.  
  57. void MyGraphicsView::wheelEvent(QWheelEvent * event) {
  58. qDebug() << event << "in my graphics view (wheelEvent).";
  59. event->ignore();
  60. }
  61.  
  62.  
  63. void MyGraphicsView::keyPressEvent(QKeyEvent * event) {
  64. qDebug() << event << "in my graphics view (keyPressEvent).";
  65. event->ignore();
  66. }
  67.  
  68. void MyGraphicsView::mousePressEvent(QMouseEvent * event) {
  69. qDebug() << event << "in my graphics view (mousePressEvent).";
  70. event->ignore();
  71. }
To copy to clipboard, switch view to plain text mode 


Result for QPaintEvent, QMouseEvent, QKeyEvent and QWheelEvent:
-----------------------------------------------------------------------------------

QPaintEvent(0xbfc7714c) in filter.
QPaintEvent(0xbfc7714c) in my graphics view (event).
QPaintEvent(0xbfc76f0c) in my graphics view (paintEvent).

QMouseEvent(MouseButtonPress, 1, 1, 0) in my graphics view (mousePressEvent).
QMouseEvent(MouseButtonPress, 1, 1, 0) in filter.
QMouseEvent(MouseButtonPress, 1, 1, 0) in my graphics view (event).

QKeyEvent(KeyPress, 1000013, 0, """", false, 1) in filter.
QKeyEvent(KeyPress, 1000013, 0, """", false, 1) in my graphics view (event).
QKeyEvent(KeyPress, 1000013, 0, """", false, 1) in my graphics view (keyPressEvent).

QWheelEvent(120) in my graphics view (wheelEvent).
QWheelEvent(120) in filter.
QWheelEvent(120) in my graphics view (event).