15

I've just the documentation on the Qt event system and the QEvent class. I'm interested in the behavior of the QObject::event() method. The documentation states:

This virtual function receives events to an object and should return true if the event e was recognized and processed.

What is the expected behavior when false is returned from the event() method? What else is attempted in order to handle the event? Is the event automatically forwarded to the parent object?

Note: I know the source is available, and I do have a copy. I'm ideally looking for some piece of documentation addressing this behavior.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
André Caron
  • 44,541
  • 12
  • 67
  • 125

2 Answers2

8

I believe the best practice is to explicitly forward the events to the base-class event method if you do not wish to filter that event type (e.g. return QObject::event(event);) since the event function delegates events to specific handlers (e.g. QWidget::keyPressEvent).

QCoreApplication::notify propogates events based on the return value. On true, it considers the event as consumed and stops. Otherwise, the event is passed to the object's parent. For more information, see Events and Filters and Another Look at Events.

Keithel
  • 253
  • 2
  • 12
Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
  • What does "propagates events based on the return value" mean? This seems obvious at first, but where else does it propagate to? There is only one receiver to the event. How would it know what receiver to use instead? – André Caron May 10 '11 at 00:08
  • This best practice is well documented in the cited links, but it doesn't answer the question. Who uses this return value? – André Caron May 10 '11 at 00:09
  • Look at the source code for QCoreApplication::notify. It uses the return value of QObject::event. Events are propagated to an object's parent if it has one. – Judge Maygarden May 10 '11 at 01:05
  • It knows where to propagate the event because every object has a parent pointer. – Judge Maygarden May 10 '11 at 01:23
  • Didn't notice that part. It says "For certain types of events (e.g. mouse and key events), the event will be propagated to the receiver's parent and so on up to the top-level object if the receiver is not interested in the event (i.e., it returns false).". Unfortunately, the documentation is not clear: it only applies for *certain type of events*, whatever that means. – André Caron May 10 '11 at 13:13
0

Some Events can be propagated.Event will be propagated to it's parent and it's parent recursively until it is processed. Take a look at this:https://doc.qt.io/archives/qq/qq11-events.html

Prinz Km
  • 323
  • 1
  • 12