QAction shortcuts not working in Qt5 and Mac OS
-
I have a Qt5 application that was developed on Debian Linux. I am porting it to Mac OS and enhancing it. When I ported it, all of the menu action shortcuts came over and had been changed in Qt Creator to reflect Mac's command key rather than the control key. The XML in the ui file still looks the same.
<action name="LoginAction">
<property name="text">
<string>Log in...</string>
</property>
<property name="shortcut">
<string>Ctrl+L</string>
</property>
</action>However, the only shortcut that works is command-Q. I have tried adding the shortcuts programmatically:
#if defined (Q_OS_MACX)
ui->LoginAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
#endifbut they still don't work.
Any help would be appreciated.
-
What is your version of Qt ?
If you click on the menu, is the action triggered ?
You can try with a predefined key sequence to see if it works:
QAction* action=menu->addAction(tr("New")); action->setShortcut(QKeySequence::New);
You need to create a default menu with no parent, in the case no more window is opened.
-
The issue is with using a menu that is not native to OS X. The shortcuts from the non-native menu actions don't seem to trigger anything. In the UI designer view, you need to to check the native menu box in the top level menu properties, and you need to make the shortcuts application level shortcuts. There is a checkbox on the QAction properties for this.
You can also do it programmatically like so:
QAction *myAction = new QAction("My action"); myAction->setShortcut(QKeySequence("CTRL+M")); myAction->setShortcutContext(Qt::ApplicationShortcut); QMenu *myMenu = new QMenu("My Menu"); myMenu->addAction(myAction); ui->menuBar->addMenu(myMenu); ui->menuBar->setNativeMenuBar(true);