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.
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));
but they still don't work.
Any help would be appreciated.
No joy. It did change the modifier symbol from the command thingie to a carat, but the shortcut still doesn't work. Even tried control-L.
My bad, Qt::META is the CTRL key on Mac.
I'm using this in one of my app:
On Mac it becomes ⇧⌘B
Still no luck.
In the code I posted, I had just used the enumerators rather than a string, but neither method works. Adding the invocation of setShortcutContext didn't solve the problem.
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.
Still not working. This leads me to believe that the problem is with Cocoa grabbing the key sequence.
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);