QAction shortcut doesnt always work

放肆的年华 提交于 2019-12-05 01:09:38

You need to add the action to a widget, since it's the widget that will be listening for key events. Assuming "this" is a mainwindow, simply do

addAction(deleteAct);

Note that you can add the same action to multiple widgets (that's the whole point of the separated action concept). So it's fine to add it to the mainwindow and to a menu.

Try changing the shortcut context of the action, for example:

deleteAct->setShortcutContext(Qt::ApplicationShortcut);

Without seeing the complete code, I'd hazard a guess that somewhere it gets enabled/disabled. Make sure that the shortcut is getting hit in the constructor and not 'disabled' somewhere else because of a setting perhaps.

You can use http://doc.qt.io/qt-5/qaction.html#shortcutVisibleInContextMenu-prop property since QT 5.10 for this:

deleteAct->setShortcutVisibleInContextMenu(true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!