在 Qt 中,如何在菜单中显示键盘快捷键但禁用它们?
我正在向主窗口的菜单中添加一堆 QAction
。这些操作也可以通过键盘触发,并且我希望快捷方式在菜单中像往常一样可见,例如
-----------------
|Copy Ctrl+C|
-----------------
我可以使用QAction.setShortcut()
来执行此操作。但是,我不希望这些QAction
由快捷方式触发;我在其他地方单独处理所有键盘输入。
这可能吗?我可以禁用 QAction 中的快捷方式,但菜单中仍保留快捷方式文本(在此示例中为 Ctrl + C)吗?
编辑:我最终执行此操作的方式是连接到菜单的 aboutToShow()
和 aboutToHide()
事件,并启用/禁用快捷方式因此它们仅在显示菜单时才处于活动状态。但我希望有一个更清洁的解决方案......
I'm adding a bunch of QAction
s to my main window's menus. These actions can also be triggered by the keyboard, and I want the shortcut to be visible in the menu, as usual, e.g.
-----------------
|Copy Ctrl+C|
-----------------
I can do this using QAction.setShortcut()
. However, I don't want these QAction
s to be triggered by the shortcuts; I'm handling all keyboard input separately elsewhere.
Is this possible? Can I disable the shortcut in the QAction but still have the shortcut text (in this example Ctrl + C) in my menus?
EDIT: The way I ended up doing it is connecting to the menu's aboutToShow()
and aboutToHide()
events, and enabling/disabling the shortcuts so they are only active when the menu is shown. But I'd appreciate a cleaner solution...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以继承 QAction 并覆盖 QAction::event(QEvent*):
这将导致发送到您的操作的 QEvent::Shortcut 类型的任何事件都不会触发“triggered()”信号。
You could inherit from QAction and override QAction::event(QEvent*):
This will cause any events of type QEvent::Shortcut sent to your actions to not trigger the 'triggered()' signals.
这看起来像是带有快捷方式的操作,但实际上并未安装快捷方式。
这是一个完整的示例:
This will look like an action with a shortcut, but the shortcut is not actually installed.
Here is a full example: