Discussion:
[Interest] Capture keyPressEvent on QMenuBar/QMenu?
Israel Brewster
2018-10-10 20:07:20 UTC
Permalink
I want to modify a QAction if the user holds down a specific key. You can see similar behavior on the Mac, for example, if you pull down the Apple menu and press/release the option key - "About This Mac" changes to "System Information...", among other things. This is the behavior I want to emulate in my application.

To that end, I tried overriding the keyPressEvent on both the QMenuBar and the QMenu containing the QAction. However, my debugging indicated that neither of these keyPressEvent functions were called when pressing any key with the relevant QMenu displayed.

I'm sort of wondering if this is perhaps due to the event being handled at a lower level in order to provide "type ahead" type functionality in the menu? I did notice that different menu entries are selected when typing various keys, which I believe to be normal behavior.

Regardless, how can I respond to a keyPress type event in a QMenuBar or QMenu?

-----------------------------------------------
Israel Brewster
Tony Rietwyk
2018-10-11 00:02:53 UTC
Permalink
Hi Israel,

Try installEventFilter on the menu to see the KeyPress events before
they are handled by the menu.

Regards, Tony
Post by Israel Brewster
I want to modify a QAction if the user holds down a specific key. You
can see similar behavior on the Mac, for example, if you pull down the
Apple menu and press/release the option key - "About This Mac" changes
to "System Information...", among other things. This is the behavior I
want to emulate in my application.
To that end, I tried overriding the keyPressEvent on both the QMenuBar
and the QMenu containing the QAction. However, my debugging indicated
that neither of these keyPressEvent functions were called when
pressing any key with the relevant QMenu displayed.
I'm sort of wondering if this is perhaps due to the event being
handled at a lower level in order to provide "type ahead" type
functionality in the menu? I did notice that different menu entries
are selected when typing various keys, which I believe to be normal
behavior.
Regardless, how can I respond to a keyPress type event in a QMenuBar or QMenu?
-----------------------------------------------
Israel Brewster
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Israel Brewster
2018-10-11 16:36:41 UTC
Permalink
On Oct 11, 2018, at 2:54 AM, Frederik Christiani via Interest <***@qt-project.org<mailto:***@qt-project.org>> wrote:

On 11-10-2018 02:02, Tony Rietwyk wrote:
Hi Israel,
Try installEventFilter on the menu to see the KeyPress events before they are handled by the menu.

In the eventFilter you'll want to look at the events of type
QEvent::ShortcutOverride. Remember to accept() the event if you don't want the shortcut action triggered.



Ok, I tried with using an eventFilter, as per the documentation at http://doc.qt.io/qt-5/qobject.html#installEventFilter. As a test, I used their example code exactly for the implementation of the KeyPressEater class. I tried this in two ways: 1) installing the event filter on my QMenuBar object, and 2) installing the eventFilter on the base QApplication object, as per the documentation at http://doc.qt.io/qt-5/eventsandfilters.html#event-filters, which says:

"It is also possible to filter all events for the entire application, by installing an event filter on the QApplication or QCoreApplication object. Such global event filters are called before the object-specific filters."

When installed on my QMenuBar object, the eventFilter function was *never* called, as far as I can determine. When installed on the QApplication object, the eventFilter function *was* called as expected - right up until I activated a menu. ANY menu - even the Apple menu. At that point, the eventFilter function stopped receiving events.

I am running Qt 5.9.6 on MacOS X 10.13 and 10.14 (tried both with the same behavior). It sort of appears that the system is taking over the menu bar entirely, such that events simply don't make it to my application *at all* when the menu is activated. Is something like that a possibility?

-----------------------------------------------
Israel Brewster
Systems Analyst II
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------
Kind regards,
Frederik

--
Frederik Christiani
Viking Software
https://www.vikingsoftware.com/
Israel Brewster
2018-10-11 17:42:06 UTC
Permalink
On Oct 11, 2018, at 8:36 AM, Israel Brewster <***@flyravn.com<mailto:***@flyravn.com>> wrote:



On Oct 11, 2018, at 2:54 AM, Frederik Christiani via Interest <***@qt-project.org<mailto:***@qt-project.org>> wrote:

On 11-10-2018 02:02, Tony Rietwyk wrote:
Hi Israel,
Try installEventFilter on the menu to see the KeyPress events before they are handled by the menu.

In the eventFilter you'll want to look at the events of type
QEvent::ShortcutOverride. Remember to accept() the event if you don't want the shortcut action triggered.



Ok, I tried with using an eventFilter, as per the documentation at http://doc.qt.io/qt-5/qobject.html#installEventFilter. As a test, I used their example code exactly for the implementation of the KeyPressEater class. I tried this in two ways: 1) installing the event filter on my QMenuBar object, and 2) installing the eventFilter on the base QApplication object, as per the documentation at http://doc.qt.io/qt-5/eventsandfilters.html#event-filters, which says:

"It is also possible to filter all events for the entire application, by installing an event filter on the QApplication or QCoreApplication object. Such global event filters are called before the object-specific filters."

When installed on my QMenuBar object, the eventFilter function was *never* called, as far as I can determine. When installed on the QApplication object, the eventFilter function *was* called as expected - right up until I activated a menu. ANY menu - even the Apple menu. At that point, the eventFilter function stopped receiving events.

I am running Qt 5.9.6 on MacOS X 10.13 and 10.14 (tried both with the same behavior). It sort of appears that the system is taking over the menu bar entirely, such that events simply don't make it to my application *at all* when the menu is activated. Is something like that a possibility?


In digging deeper into the documentation, I found this: http://doc.qt.io/qt-5/osx-issues.html#limitations, which states:

"QMenu objects used in the native menu bar are not able to handle Qt events via the normal event handlers. Install a delegate on the menu itself to be notified of these changes"

So I guess I need to install a delegate on the menu itself. Can someone point me to the documentation for that? Thanks.
-----------------------------------------------
Israel Brewster
Systems Analyst II
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------

-----------------------------------------------
Israel Brewster
Systems Analyst II
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------
Kind regards,
Frederik

--
Frederik Christiani
Viking Software
https://www.vikingsoftware.com/

Loading...