qt - 检查后代在 QML 中是否具有 activeFocus == true
问题描述
在深度嵌套的 QML GUI 中,是否有一种简单的方法可以找出 Item 的任何子或孙等是否有activeFocus == true
?
Item {
id: intermediateItem
visible: anyDescendantHasActiveFocus(intermediateItem) ? true : false
Item {
Item {
Item {
id: hasActiveFocus
Component.onCompleted: hasActiveFocus.forceActiveFocus()
}
}
}
}
解决方案
无需从基础祖先向下钻取层次结构,您可以从当前向上activeFocusItem
:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
function anyDescendantHasActiveFocus(ancestor) {
let item = ancestor.Window.activeFocusItem;
while (item) {
if (item === ancestor)
return true;
item = item.parent;
}
return false;
}
Row {
anchors.centerIn: parent
spacing: 10
Repeater {
model: 3
Rectangle {
width: 200
height: 100
border.width: 1
border.color: anyDescendantHasActiveFocus(this) ? "red" : "black"
Rectangle {
anchors.fill: parent; anchors.margins: 10
border.width: 1
Rectangle {
anchors.fill: parent; anchors.margins: 10
border.width: 1
Rectangle {
anchors.fill: parent; anchors.margins: 10
border.width: 1
Button {
anchors.centerIn: parent
text: "Focus me"
}
}
}
}
}
}
}
}
推荐阅读
- c# - 处理 Xamarin.Android 应用程序的本机库中发生的异常
- networking - 使用 linux 内核函数的正确 udp csum
- python - 如何在 Python 中返回两个多维列表的差异?
- python - 如何将 gRPC 与 asyncio 一起使用
- javascript - jQuery - 添加到序列化数组
- arrays - 将项目添加到数组 (VB 2008)
- python - 为什么 break 语句没有退出我的 while 循环?
- http - 向 Struct 添加一些日期,然后将其放入 go 模板中
- c# - c# 防止不必要的api调用
- node.js - Nodejs - 护照,完成不是一个功能