Reputation: 2713
I read the documentation about:
focus
propertyactiveFocus
propertyforceActiveFocus()
methodFocusScope
objectbut it is still not clear when should someone use forceActiveFocus()
method over setting the focus
property to true or vice versa.
Upvotes: 13
Views: 20580
Reputation: 13711
As the documentation states:
For very simple cases simply setting the focus property is sometimes sufficient.
Such a simple case would be, if the Item
which gets focus: true
is not enclosed by a FocusScope
that might have not the focus.
Then it continues with:
> Within each focus scope one object may have Item::focus set to true. If more than one Item has the focus property set, the last type to set the focus will have the focus and the others are unset, similar to when there are no focus scopes.
> When a focus scope receives active focus, the contained type with focus set (if any) also gets the active focus. If this type is also a FocusScope, the proxying behavior continues. Both the focus scope and the sub-focused item will have the activeFocus property set.
From where we learn, about the fact that setting focus: true
is not sufficient, if it is for an Item
that is a successor to a FocusScope
as this FocusScope
would need to have activeFocus
su that the successor Item
will receive activeFocus
. This is recursive, and means, that the FocusScope
will need to have focus: true
and a possible predecessor FocusScope
needs the activeFocus
and so on. Which results in some kind of focus tree
This focus tree consists out of inner nodes that are the FocusScope
s and leafs that are Item
s. A FocusScope
might be a leaf as well, but I don't know why it should.
In this tree, each FocusScope
may have up to one child node (either Item
(leaf) or FocusScope
(inner node) that has focus === true
. Traversing this tree, following the path where all traversed nodes have focus === true
the traversed nodes have also activeFocus === true
. As each FocusScope
may have only up to one child node with focus === true
there is only one such path.
Column {
FocusScope {
focus: false
width: 100
height: 100
Text {
focus: true
text: 'has focus ' + focus + '\nhas activeFocus ' + activeFocus
}
}
FocusScope {
focus: true
width: 100
height: 100
Text {
focus: true
text: 'has focus ' + focus + '\nhas activeFocus ' + activeFocus
}
}
}
Here we have two FocusScope
s. Both have a child that has focus
, but as only the second FocusScope
has the focus
itself, it's child has activeFocus
.
The use of forceActiveFocus()
traverses the focus tree, and sets focus
to true
for each node on the way, so the Item
has activeFocus
at the end.
Upvotes: 18