QAction *action = new QAction("My Action", this); action->setShortcut(Qt::Key_A); action->setShortcutContext(Qt::WidgetShortcut);
QAction *action = new QAction("My Action", this); action->setShortcut(Qt::Key_A); action->setShortcutContext(Qt::ApplicationShortcut); QMenu *menu = new QMenu(this); menu->addAction(action);In this example, we create a new QAction named "My Action" and set the shortcut key as the letter A. We also specify the shortcut context as ApplicationShortcut, which means that the shortcut key will be active even if the specified widget is not in focus. We then add the action to a QMenu and display it in the UI. Package/Library: Qt C++ library.