KeyNavigation QML Type
支持箭头键导航。更多
Import Statement: | import QtQuick |
附加属性
详细说明
基于按键的用户界面通常允许使用箭头键在可聚焦的项目间导航。KeyNavigation 附加属性通过提供一种方便的方法来指定按下箭头键或制表符键时应获得焦点的项目,从而实现这种行为。
下面的示例为 2x2 网格的项目提供了按键导航功能:
import QtQuick Grid { width: 100; height: 100 columns: 2 Rectangle { id: topLeft width: 50; height: 50 color: focus ? "red" : "lightgray" focus: true KeyNavigation.right: topRight KeyNavigation.down: bottomLeft } Rectangle { id: topRight width: 50; height: 50 color: focus ? "red" : "lightgray" KeyNavigation.left: topLeft KeyNavigation.down: bottomRight } Rectangle { id: bottomLeft width: 50; height: 50 color: focus ? "red" : "lightgray" KeyNavigation.right: bottomRight KeyNavigation.up: topLeft } Rectangle { id: bottomRight width: 50; height: 50 color: focus ? "red" : "lightgray" KeyNavigation.left: bottomLeft KeyNavigation.up: topRight } }
通过将focus 设置为true
,左上角的项目最初获得焦点。当按下箭头键时,焦点将移动到相应的项目,该项目由 KeyNavigationleft 、right 、up 或down 属性的设置值定义。
请注意,如果 KeyNavigation 附属属性接收到所请求的方向键或制表符键的按下和释放事件,则该事件将被接受,不会继续传播。
默认情况下,KeyNavigation 会在其所附加的项目之后接收按键事件。如果项目接受了按键事件,KeyNavigation 附加属性将不会接收该按键的事件。将priority 属性设置为KeyNavigation.BeforeItem
可使事件用于项目之前的按键导航,而不是之后的按键导航。
如果焦点切换到的项目未启用或不可见,则会尝试跳过此项目,将焦点放在下一个项目上。如果有一连串具有相同按键导航处理程序的项目,就有可能出现这种情况。如果一行中的多个项目未启用或不可见,它们也将被跳过。
KeyNavigation 将隐式设置另一个方向,以便将焦点返回到此项目。因此,如果您将left 设置为另一个项目,right 将被设置为该项目的 KeyNavigation,从而将焦点返回到该项。但是,如果该项目的 "关键导航 "已明确设置了右键,则不会发生任何变化。这意味着上面的示例可以在不指定任何项目的 KeyNavigation.right 或 KeyNavigation.down 的情况下实现相同的行为。
另请参见 Keys attached property 。
附加属性文档
KeyNavigation.backtab : Item |
该属性用于保存按下 Shift+Tab 组合键(Backtab)时要分配焦点的项目。
KeyNavigation.down : Item |
该属性用于保存按向下光标键时要分配焦点的项目。
KeyNavigation.left : Item |
该属性用于保存按下左光标键时分配焦点的项目。
KeyNavigation.priority : enumeration |
该属性决定了按键处理是在附加项自身按键处理之前还是之后进行。
常数 | 说明 |
---|---|
KeyNavigation.BeforeItem | 在正常的项目按键处理之前处理按键事件。如果事件用于按键导航,则会被接受,不会传递给项目。 |
KeyNavigation.AfterItem | (默认)在正常的项目按键处理之后处理按键事件。如果项目接受按键事件,则不会由KeyNavigation 附加属性处理程序进行处理。 |
KeyNavigation.right : Item |
该属性用于保存按下右光标键时要分配焦点的项目。
KeyNavigation.tab : Item |
该属性用于保存按 Tab 键时分配焦点的项目。
KeyNavigation.up : Item |
该属性用于保存按向上光标键时要分配焦点的项目。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.