I'm new to QML and I'm confused by the FocusScope type. My understanding is that it's used to control the focus of part an application when multiple items are requesting focus.
The documentation says:
Conceptually focus scopes are quite simple.
Within each focus scope one element may have Item::focus set to true. If more than one Item has the focus property set, the last element to set the focus will have the focus and the others are unset, similar to when there are no focus scopes.
So, when the program is run, why is the first textfield in the following code focused and not the last?
import QtQuick 2.0
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.15
ApplicationWindow{
visible: true
width: 200
height: 200
FocusScope{
Column{
Text{text: "first focus scope"}
TextField {
width: 100; height: 25; focus: true
text: focus
}
TextField{
width: 100; height: 25; focus: true
text: focus
}
Text{text: "second focus scope"}
TextField {
width: 100; height: 25; focus: true
text: focus
}
TextField{
width: 100; height: 25; focus: true
text: focus
}
}
}
}
Any help is greatly appreciated!
The order in which properties are set is undefined:
You can see which order they're actually set in with the following code:
This is the output:
You can only have one item with active focus at a time, so setting focus to true for more than one sibling doesn't make much sense.
If you want to e.g. have the first
TextFieldhave focus on startup, I'd just do this: