QAction 快捷方式并不总是有效
我在菜单项上有一个 Qaction,用于删除我的视图之一中选定的项目。以下是我创建操作的方法:
deleteAct = new QAction( tr("Delete Selected"), this);
deleteAct->setShortcut(QKeySequence::Delete);
connect(deleteAct, SIGNAL(triggered()), this, SLOT(deleteSelected()));
我设置了一个键盘快捷键(删除键),它应该触发 delectAct 操作。它在大多数情况下都有效,但在某些时候它会停止工作...有谁知道为什么快捷方式会停止工作?
注意:如果我从菜单项触发该操作,该操作仍然有效。它只是捷径,不...
I have a Qaction on a menu item for deleting selected items in one of my views. Here is how i create the action:
deleteAct = new QAction( tr("Delete Selected"), this);
deleteAct->setShortcut(QKeySequence::Delete);
connect(deleteAct, SIGNAL(triggered()), this, SLOT(deleteSelected()));
I setup a keyboard shortcut (Delete Key) which should trigger the delectAct action. It works most of the time but at some points it stops working... Does anyone know why the shortcut would stop working?
Note: the action still works if i trigger it from the menu item. Its just the shortcut that doesn't...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将操作添加到小部件,因为该小部件将侦听关键事件。
假设“this”是一个主窗口,只需
注意,您可以将相同的操作添加到多个小部件(这就是分离操作概念的全部要点)。因此,将其添加到主窗口和菜单中就可以了。
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
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:
该快捷方式的工作原理取决于应用程序视图的焦点。
我想要在按钮上使用快捷方式。
在我的应用程序中,我更改了操作的快捷方式上下文,
将操作添加到小部件
最后到应用程序的子视图。
然后必须连接小部件动作所需的信号和插槽。
The shortcut works depending on the focus of the application views.
I wanted to have shortcuts working on buttons.
In my application I changed the shortcut context of the action,
added the action to the widget
and finally to the subviews of the application.
Then the necessary signals and slots of widget an action must be connected.
您可以使用 http://doc.qt.io/qt-5自 QT 5.10 起 /qaction.html#shortcutVisibleInContextMenu-prop 属性:
You can use http://doc.qt.io/qt-5/qaction.html#shortcutVisibleInContextMenu-prop property since QT 5.10 for this: