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.
An Item requests focus by setting the focus property to true . For very simple cases simply setting the focus property is sometimes sufficient. If we run the following example with the qml tool, we see that the keyHandler type has active focus and pressing the A , B , or C keys modifies the text appropriately.
Now QML gives you chance to change the stacking order through z property of the item. in the above example if I assign z property of the red rectangle to have a value of anything above 0, I would see it on top of blue rectangle. So z property has changed the stacking order for the sibling item.
Loader is used to dynamically load QML components. Loader can load a QML file (using the source property) or a Component object (using the sourceComponent property).
All QML objects have an object parent, which is determined by the object hierarchy in which the object is declared.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With