QAction快捷方式并不总是有效 [英] QAction shortcut doesnt always work

查看:465
本文介绍了QAction快捷方式并不总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个菜单项上有一个Qaction,用于删除我的一个视图中的选定项.这是我创建动作的方法:

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()));  

我设置了一个键盘快捷方式(Delete Key),该快捷方式应该触发delectAct动作.它在大多数时间都有效,但是在某些时候它会停止工作...有人知道快捷键为什么会停止工作吗?

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...

推荐答案

您需要将操作添加到小部件,因为它将是侦听关键事件的小部件. 假设"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

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.

这篇关于QAction快捷方式并不总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆