问题描述
我的QML程序有问题.在顶层main.qml文件中,我有一些类似这样的键盘快捷键:
I'm having an issue with my QML program. In the top-level main.qml file, I have some keyboard Shortcuts like so:
Shortcut {
sequence: "Up"
onActivated: {
// yadda yadda
}
}
在另一个文件中,我有几个Keys.onPressed调用,如下所示:
In another file, I have several Keys.onPressed calls like this:
Keys.onPressed: {
if (event.key == Qt.Key_Up) {
// yadda yadda
}
}
显然,快捷方式会干扰OnPressed调用.当我注释掉快捷方式时,OnPressed的工作正常.但是当它们都处于活动状态时,快捷键似乎会拦截键盘按下并阻止OnPressed激活.
Apparently, the Shortcuts are interfering with the OnPressed calls. When I comment out the Shortcuts, the OnPressed's work fine. But when they're both active, it seems the Shortcut intercepts the keyboard press and prevents the OnPressed from activating.
我知道在发生鼠标事件时,Qt具有"accepted"变量.如果希望事件继续在堆栈中向下传播,则只需在OnActivated函数中设置"accepted = false"即可.但是,我没有在Shortcuts API中看到任何等效的接受"变量.还有其他方法可以确保事件正确传播吗?
I know that with mouse events, Qt has the "accepted" variable. If you want an event to continue propagating down the stack, you can just set "accepted = false" in the OnActivated function in order to accomplish this. However, I am not seeing any equivalent "accepted" variable in the Shortcuts API. Is there some other way I can ensure that the event is propagated correctly?
推荐答案
要像快捷方式一样,Shortcut
必须具有比活动焦点项中的键处理程序更高的优先级.否则,它将与普通的密钥处理程序没有什么不同.但是,有时像您一样需要覆盖快捷方式.
In order to act like a shortcut, Shortcut
must have a higher priority than key handlers in active focus items. Otherwise it would be no different to a normal key handler. However, sometimes there is a need to override a shortcut, like you do.
在Qt 5.8和更低版本中,您可以禁用 a Shortcut
,以防止其在某些情况下处理快捷方式事件.例如:
In Qt 5.8 and earlier, you can disable a Shortcut
to prevent it processing shortcut events under certain conditions. For example:
Shortcut {
enabled: !someItem.activeFocus
}
在Qt 5.9中,为此引入了更好的机制.现在,具有键处理程序的活动焦点项目可以通过使用 Keys.shortcutOverride
.例如:
In Qt 5.9, a better mechanism has been introduced for this. Active focus items with key handlers can now override shortcuts by accepting shortcut override events using Keys.shortcutOverride
. For example:
Item {
focus: true
Keys.onShortcutOverride: {
if (event.key == Qt.Key_Up)
event.accepted = true
}
Keys.onUpPressed: ...
}
这篇关于QML键盘快捷键会干扰按键OnPressed事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!