event filter not called before menu shortcut dispatch
-
in my QApplication i have a key-down filter:
class QKJams : public QApplication { typedef QApplication _inherited; public: QKJams(int argc, char *argv[]) : _inherited(argc, argv) { installEventFilter(this); } bool event(QEvent *eventP) { QEvent::Type eventType(eventP->type()); switch (eventType) { case QEvent::KeyPress: { • <-- breakpoint handledB = _inherited::event(eventP); } break; } return handledB; } bool eventFilter(QObject *objP, QEvent *eventP) { bool handledB = false; switch (eventP->type()) { case QEvent::KeyPress: { • <-- breakpoint handledB = _inherited::eventFilter(objP, eventP); } break; } return handledB; } };
yet keyboard shortcuts are sent to the menu handler BEFORE the app either filters OR handles the event. the breakpoints above are not hit when i press a key that happens to be an unmodified shortcut key in the menu bar.
the doc says the App should get a crack at the event BEFORE any other processing, which i took to mean before looking in the menu bar for a matching key shortcut.
-
update: only if i remove the key shortcut from the menu, then it works, but backwards from what i thought it should do: first the Window filter get called, then the App filter. i thought the App filter would get called FIRST, then window, then menubar?
-
update: only if i remove the key shortcut from the menu, then it works, but backwards from what i thought it should do: first the Window filter get called, then the App filter. i thought the App filter would get called FIRST, then window, then menubar?
@davecotter said in event filter not called before menu shortcut dispatch:
first the Window filter get called
Hi David,
Unless I have missed something here: I don’t find that “Window filter” in the code.
Cheers
Axel -
well i didn't post the full project. but it is there, trust me.
my question is: is it expected that it goes: menus, windows, app ? and not the other way around?
-
well i didn't post the full project. but it is there, trust me.
my question is: is it expected that it goes: menus, windows, app ? and not the other way around?
Hi, you say it stops working when you add the menu shortcut?!
what's the context of your menu shortcut?and not the other way around?
Propagation in the
QObject
tree is bottom up, starting with the "inner-most" child at event "position".If you want to handle the same application-wide shortcut with your menu, try to set it to
Qt::ApplicationShortcut
otherwise theQt::WindowShortcut
from your menu in yourWindow
will accept and eat the key/shortcut event and it will never reach your application handler...
There is an article in the Qt Documentation about shortcut contexts and ambiguity... can't find it right now.Btw:
@davecotter said in event filter not called before menu shortcut dispatch:
installEventFilter(this);
This line, which is short for
this->installEventFilter(this);
doesn't make too much sense :)
AQObject
doesn't need aneventFilter
to watch its own events, it will receive and handle them anyway usingQObject::event()
and all the more specific event handlers (likeQWidget::mouseMoveEvent
or key-press-Events... etc.). -
well i didn't post the full project. but it is there, trust me.
my question is: is it expected that it goes: menus, windows, app ? and not the other way around?
@davecotter
As @Pl45m4 has written, events go to the "most specific, innermost" widget first. If that handles/accepts the event that's it; if it does not/rejects then the next outer widget gets to have a go, and so on up the hierarchy tree. If you define something like anApplicationShortcut
that by-passes the usual handling and goes straight to that handler. -
so are menu bar keyboard shortcuts considered the "most specific" ?
-
I guess that's my question 👆🏻
-
I guess that's my question 👆🏻
and that 👉 https://forum.qt.io/post/808209 is my answer.
Have you tried it? Does it work?