FocusScope doesn't give the child focus properly
-
Hi all,
In the FocusScope example here using the following code, there's no focus shown when running the project. The tab key doesn't give/change the focus either!
TLineEditV1.qml:
import QtQuick 2.12 FocusScope { width: 96; height: input.height + 8 Rectangle { anchors.fill: parent color: "lightsteelblue" border.color: "gray" } property alias text: input.text property alias input: input TextInput { id: input width: 100; height: 20 anchors.fill: parent anchors.margins: 4 focus: true } }
mani.qml:
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 Window { visible: true width: 600; height: 400 color: "linen" TLineEditV1 { id: input1 anchors.top: parent.top anchors.topMargin: 10 text: "text 1" } TLineEditV1 { id: input2 anchors.top: input1.bottom anchors.topMargin: 5 text: "text 2" } }
What is the problem, please?
-
Hi @tomy , inside your FocusScope give this:-
To have focus on tab give this:-
activeFocusOnTab :true
To default focus when you launch the project give this:-
focus: true
-
To default focus when you launch the project give this:-
focus: trueIt's set to true there, beforehand. But no cursor blinking inside any of the text inputs!
-
Hi @tomy , have you made the 2 changes inside the FocusScope ?
Because i have just copy pasted your code and added the 2 lines which i mentioned above, when launched the default focus is on the first TextInput and when tab is pressed the focus moves to the next TextInput.
Output:-
-
Yes, but
activeFocusOnTab :true
is not mentioned in the book as far as I saw, so if it's mandatory, why is it not written there!? -
Hi @tomy , its mentioned in the documentation[https://doc.qt.io/qt-5/qml-qtquick-item.html#activeFocusOnTab-prop]
By default its set to false.
-
Apart from the Tab key, why don't we have a default focus while it's set?
-
Hi @tomy , you need to set the focus, as i see from your code you have not set the focus anywhere.
Everything is clearly mentioned in the documentation with examples please have look once[https://doc.qt.io/qt-5/qml-qtquick-item.html#focus-prop]
-
It's previously set. Please look at TLineEditV1.qml once again:
... TextInput { id: input width: 100; height: 20 anchors.fill: parent anchors.margins: 4 focus: true // <= Here } ...
You can also test it yourself.
-
Hi @tomy , you have set the focus to the TextInput not the FocusScope, so TextInput will gain active focus only when FocusScope gains active focus.
-
@Shrinidhi-Upadhyaya
Thank you, now solved.
The book sucks at times. It hasn't mentioned this.