The first item in my main menu is called "Project":
m_projectMenu
= new QMenu("Pro&ject", m_menuBar
);
m_projectMenu = new QMenu("Pro&ject", m_menuBar);
To copy to clipboard, switch view to plain text mode
So the user can hit "Alt+J" to open the menu via keyboard shortcut. Great!
For historical reasons, I'd also like the keyboard shortcut "Alt+F" to open the menu. I thought I could use a QShortcut, and hook it up to the menu's action:
QObject::connect(fileshortcut,
SIGNAL(activated
()),
m_projectMenu->menuAction(), SLOT(trigger()));
QShortcut* fileshortcut = new QShortcut(QKeySequence("Alt+F"), m_projectMenu);
QObject::connect(fileshortcut, SIGNAL(activated()),
m_projectMenu->menuAction(), SLOT(trigger()));
To copy to clipboard, switch view to plain text mode
But then pressing "Alt+F" only acts as if the mouse is hovering over the menu, rather than clicking to display the menu.
Then I thought of adding multiple keyboard shortcuts to the menu:
QList<QKeySequence> shortcuts;
m_projectMenu->menuAction()->setShortcuts(shortcuts);
QList<QKeySequence> shortcuts;
shortcuts.push_back(QKeySequence("Alt+F"));
shortcuts.push_back(QKeySequence("Alt+J"));
m_projectMenu->menuAction()->setShortcuts(shortcuts);
To copy to clipboard, switch view to plain text mode
But that doesn't work either. "Alt+F" does nothing, and "Alt+J" results in messages in stdout/stderr:
QAction::eventFilter: Ambiguous shortcut overload: Alt+J
So what's the correct way to do this? It seems that menuAction() does not return what I thought it returned, but the docs only say "Returns the action associated with this menu." Is that not the action on which keyboard shortcuts should be installed?
Bookmarks