问题
I have a program that in main menu uses certain shortcuts. It works in windows and Linux. When porting to mac, certain shortcuts will not work.
The menu is created using QT Designer. It looks like this
QAction *actDelete;
actDelete = new QAction(MainWindow);
actDelete->setObjectName(QString::fromUtf8("actDelete"));
menu_Edit->addAction(actDelete);
actDelete->setText(QApplication::translate("MainWindow", "Delete", 0, QApplication::UnicodeUTF8));
actDelete->setShortcut(QApplication::translate("MainWindow", "Del", 0, QApplication::UnicodeUTF8));
QAction *act1;
act1 = new QAction(MainWindow);
act1->setObjectName(QString::fromUtf8("act1"));
menu1->addAction(act1);
act1->setText(QApplication::translate("MainWindow", "Action 1", 0, QApplication::UnicodeUTF8));
act1->setShortcut(QApplication::translate("MainWindow", "F12", 0, QApplication::UnicodeUTF8));
The only keys that don't work are the function keys, and Delete (for Delete the reason may be that Mac has replaced it with a key that technically is backspace... but it is still called Delete so it should work ?)
Other key combinations work... The function keys are assigned to other functionality it seems...
It seems that it is impossible to remove the default functionality of function keys through code (is that really true ?) - so I went and clicked the button to 'Enable "Use all F1, F2, etc. keys as standard function keys."'. At this point I can press the function keys and see what code they generate (like key=0x100003B
for ctrl + F12
or cmd + F12
).
In the main window constructor, after instantiating ui, I tried
#if defined Q_OS_MACX
m_ui->act1->setShortcut(tr("CTRL+F12")); // still nothing happening
m_ui->act1->setShortcut(Qt::CTRL+Qt::Key_F12); // same, no effect on mac (though if i put it for windows the ctrl+F12 does have the desired effect
#endif
Qt version 4.7-4.8, OSX 10.6.8 building using g++
回答1:
Added a definition of shortcuts for Mac
#if defined (Q_OS_MACX)
m_ui->act1->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F12));
#endif
Added Backspace instead of Delete for Mac.
来源:https://stackoverflow.com/questions/25852370/keyboard-shortcuts-function-keys-created-in-qt-app-dont-work-on-osx