I am experimenting with custom events in Qt Creator. I am currently examining this example code on another site:
bool MyClass::event(QEvent* e)
{
if (e && e->type() == MyCustomEventType) {
MyCustomEvent* ce = dynamic_cast<MyCustomEventType*>(e);
return handleCustomEvent(ce);
}
// very important: still handle all the other Qt events!
return QObject::event(e);
}
The conditional statement checks if the event passed is the custom event, then it executes code that it wants to happen when the event occurs. What I do not understand is return handleCustomEvent(e) (what is this function supposed to do and where is it supposed to be declared?) and what return QObject::event(e) does. From what I read on the Qt documentation, the only thing this function does is return whether the event’s function (is this handleCustomEvent?) is “recognized and processed”. Is this supposed to handle all other events in the loop?
handleCustomEvent()is the method you need to implement in your classMyClasswhich will process your Custom EventMyCustomEventType.If it’s not your custom event, the last line
return QObject::event(e);will be called to handle other events type.So the method in your snippet
bool MyClass::event(QEvent* e), is acting like a routing code, to decide where to send the event for processing, and does not actually process the events.Once decided that
'e'is of typeMyCustomEventType– it invokeshandleCustomEvent()which will contain your code to handle this event type.If not – the last line calls
QObject::event()to process it instead. This will handle all other remaining types of events.So, no, you need not worry about handling other events, unless you want to.
So, you’d declare the
handleCustomEvent()inMyClassand implement it as well.Something like:
In the implementation you may have the logic as you require – to actually do the processing for your custom-event type
MyCustomEventType.