{"id":211,"date":"2023-07-14T07:58:36","date_gmt":"2023-07-14T07:58:36","guid":{"rendered":"https:\/\/www.vcreatelogic.com\/?p=211"},"modified":"2023-07-14T08:23:28","modified_gmt":"2023-07-14T08:23:28","slug":"what-exactly-does-focusscope-do-in-qml","status":"publish","type":"post","link":"https:\/\/www.vcreatelogic.com\/index.php\/2023\/07\/14\/what-exactly-does-focusscope-do-in-qml\/","title":{"rendered":"What exactly does FocusScope do in QML?"},"content":{"rendered":"\n<p>If you look at the documentation of <a href=\"https:\/\/doc.qt.io\/qt-6\/qml-qtquick-focusscope.html\" target=\"_blank\" rel=\"noreferrer noopener\">FocusScope<\/a>, you will see this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Focus scopes assist in keyboard focus handling when building reusable QML components.<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<p>God I wish I could understand the entirety of <strong>FocusScope<\/strong> by reading this line of text. To be fair, the documentation has links to a <a href=\"https:\/\/doc.qt.io\/qt-6\/qtquick-input-focus.html#acquiring-focus-and-focus-scopes\" target=\"_blank\" rel=\"noreferrer noopener\">rather elaborate article<\/a> which explains how keyboard focus works in QML, and somewhere in that article <strong>FocusScope<\/strong> is also covered. However, honestly speaking that article will likely leave you more confused about <strong>FocusScope<\/strong>.<\/p>\n\n\n\n<p>In this blog, I want to demystify keyboard <strong>focus<\/strong> and <strong>FocusScope<\/strong>, so that you can use it with utmost confidence and you know exactly what you are getting into. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Keyboard Focus<\/h2>\n\n\n\n<p>First, let&#8217;s understand keyboard focus itself. QML&#8217;s <strong>Item<\/strong> has a <strong>focus<\/strong> property, which can be set by the developer to <strong>true<\/strong>, and by default it&#8217;s <strong>false<\/strong>. When we set an <strong>Item<\/strong>&#8216;s <strong>focus<\/strong> property to true, that item receives keyboard focus.<\/p>\n\n\n\n<p>Here is a small piece of QML code in which we define a <strong>TextBox<\/strong> component for accepting a single line of text. We then create a form using several instances of this component.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import QtQuick\nimport QtQuick.Window\n\nWindow {\n    width: 320\n    height: 240\n    visible: true\n    title: qsTr(\"Understanding Keyboard Focus\")\n    color: \"lightsteelblue\"\n\n    Grid {\n        columns: 2\n        spacing: 10\n        anchors.centerIn: parent\n\n        Text { text: \"Name: \" }\n        TextBox { }\n\n        Text { text: \"Place: \" }\n        TextBox { }\n\n        Text { text: \"Animal: \" }\n        TextBox { }\n\n        Text { text: \"Thing: \" }\n        TextBox { }\n    }\n\n    component TextBox : Rectangle {\n        width: 200\n        height: textInput.implicitHeight + 8\n        border {\n            width: textInput.focus ? 2 : 1\n            color: textInput.focus ? \"black\" : \"gray\"\n        }\n\n        TextInput {\n            id: textInput\n            width: parent.width - 8\n            anchors.centerIn: parent\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>When we execute the above QML code, we see a window like this.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-1.png\" alt=\"\" class=\"wp-image-213\" width=\"320\" height=\"268\"\/><\/figure>\n\n\n\n<p>None of the <strong>TextBox<\/strong> instances already have keyboard focus, as evidenced by the fact that we don&#8217;t see a blinking cursor anywhere.<\/p>\n\n\n\n<p>Let&#8217;s try to assign keyboard focus to the first <strong>TextBox<\/strong>, by setting it&#8217;s <strong>focus<\/strong> property to <strong>true<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Grid {\n    columns: 2\n    spacing: 10\n    anchors.centerIn: parent\n\n    Text { text: \"Name: \" }\n    TextBox {\n        <strong>focus: true<\/strong>\n    }\n\n    Text { text: \"Place: \" }\n    TextBox { }\n\n    Text { text: \"Animal: \" }\n    TextBox { }\n\n    Text { text: \"Thing: \" }\n    TextBox { }\n}<\/code><\/pre>\n\n\n\n<p>When we execute this piece of code, this is what we get.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-1.png\" alt=\"\" class=\"wp-image-213\" width=\"320\" height=\"268\"\/><\/figure>\n\n\n\n<p><strong>!!! NO DIFFERENCE !!!<\/strong><\/p>\n\n\n\n<p>We don&#8217;t see a blinking cursor still. Why? <\/p>\n\n\n\n<p>Because assigning <strong>focus<\/strong> to the <strong>TextBox<\/strong> item, caused its root <strong>Rectangle<\/strong> to receive focus and not the <strong>TextInput<\/strong> inside it. So, how do we get the <strong>TextInput<\/strong> inside it to get focus?<\/p>\n\n\n\n<p>? What if we <em>alias<\/em>ed <strong>TextBox<\/strong>&#8216;s <strong>focus<\/strong> property to its <strong>TextInput<\/strong>&#8216;s <strong>focus<\/strong> property? ?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component TextBox : Rectangle {\n    <strong>property alias focus: textInput.focus<\/strong>\n\n    width: 200\n    height: textInput.implicitHeight + 8\n    border {\n        width: textInput.focus ? 2 : 1\n        color: textInput.focus ? \"black\" : \"gray\"\n    }\n\n    TextInput {\n        id: textInput\n        width: parent.width - 8\n        anchors.centerIn: parent\n    }\n}<\/code><\/pre>\n\n\n\n<p>Well, this will cause a run-time error: ?<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color\"><code>KeyboardFocus.qml:32:9: Cannot override FINAL property<\/code><\/pre>\n\n\n\n<p>Alright, let&#8217;s try and do this then. ? Whenever the <strong>Rectangle<\/strong> gets <strong>focus<\/strong>, let&#8217;s have <strong>TextInput<\/strong>&#8216;s focus copy it. ?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component TextBox : Rectangle {\n    width: 200\n    height: textInput.implicitHeight + 8\n    border {\n        width: textInput.focus ? 2 : 1\n        color: textInput.focus ? \"black\" : \"gray\"\n    }\n\n    TextInput {\n        id: textInput\n        width: parent.width - 8\n        anchors.centerIn: parent\n        focus: parent.focus\n    }\n}<\/code><\/pre>\n\n\n\n<p>When we execute the program, we will see that the first TextBox did get focus &#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-2.png\" alt=\"\" class=\"wp-image-217\" width=\"320\" height=\"268\" srcset=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-2.png 640w, https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-2-300x251.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n<p>&#8230; but we see a warning on the console! ?<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color\"><code>KeyboardFocus.qml:39:9: QML TextInput: Binding loop detected for property \"focus\"<\/code><\/pre>\n\n\n\n<p>Why? ?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When <strong>Rectangle<\/strong>&#8216;s <strong>focus<\/strong> becomes true, it causes <strong>TextInput<\/strong>&#8216;s <strong>focus<\/strong> to become true.<\/li>\n\n\n\n<li>Which then causes <strong>Rectangle<\/strong>&#8216;s <strong>focus<\/strong> to become false, which will cause <strong>TextInput<\/strong>&#8216;s <strong>focus<\/strong> to become false &#8212;- ergo, binding loop!<\/li>\n<\/ul>\n\n\n\n<p>So, clearly this is not a good idea.<\/p>\n\n\n\n<p>Now, back to our problem statement. <strong><em>How do I get TextInput to get focus when TextBox gets focus, without causing all these binding loop problems?<\/em><\/strong> ?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enter FocusScope<\/h2>\n\n\n\n<p>On the one hand <strong>FocusScope<\/strong> is exactly like <strong>Item<\/strong>. It can be placed and sized like any <strong>Item<\/strong>. It can also be anchored exactly like an <strong>Item<\/strong>, and it has no visual appearance, exactly like <strong>Item<\/strong> which has no visual appearance either. But that&#8217;s where the similarities end. What a <strong>FocusScope<\/strong> actually does is, it forwards any <strong>focus<\/strong> it receives to that one child item within it whose <strong>focus<\/strong> property was <em>hard-coded<\/em> to <strong>true<\/strong>.<\/p>\n\n\n\n<p><strong>FocusScope<\/strong> is like a proxy. Veteran QtWidgets users would have used <a rel=\"noreferrer noopener\" href=\"https:\/\/doc.qt.io\/qt-6\/qlabel.html#setBuddy\" target=\"_blank\">QLabel::setBuddy()<\/a> to forward focus from a <strong>QLabel<\/strong> to any <strong>QWidget<\/strong> next to it. That&#8217;s kind of what <strong>FocusScope<\/strong> does. I would have actually preferred the item to be called <em>FocusProxy<\/em> (or even <em>FocusBuddy<\/em>) because that&#8217;s exactly what it does. As a QML developer, you can package a complex component inside a <strong>FocusScope<\/strong>, in which one of the child items has its <strong>focus<\/strong> property set to <strong>true<\/strong>. The item whose <strong>focus<\/strong> property is set to <strong>true<\/strong> will receive focus, when the root <strong>FocusProxy<\/strong> receives keyboard focus. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component TextBox : FocusScope {\n    width: 200\n    height: textInput.implicitHeight + 8\n\n    Rectangle {\n        id: background\n        anchors.fill: parent\n        border {\n            width: textInput.focus ? 2 : 1\n            color: textInput.focus ? \"black\" : \"gray\"\n        }\n\n        TextInput {\n            id: textInput\n            width: parent.width - 8\n            anchors.centerIn: parent\n\n            <strong>\/\/ We mark this TextInput as the item\n           \/\/ which is supposed to get focus when\n           \/\/ the FocusScope of the TextBox gets\n           \/\/ focus.<\/strong>\n            focus: true\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Said in other words, <strong>FocusScope<\/strong> receives <strong>focus<\/strong> only to <em>forward<\/em> it to that one child item within it whose <strong>focus<\/strong> property was set to <strong>true<\/strong>.<\/p>\n\n\n\n<p>Now, when we run the application we will see that the first <strong>TextBox<\/strong> actually received keyboard focus, because it has a blinking cursor &#8230;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-3.png\" alt=\"\" class=\"wp-image-225\" width=\"320\" height=\"268\" srcset=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-3.png 640w, https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-3-300x251.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n<p>&#8230; but all the <strong>TextBox<\/strong>es have thick borders !!!!<\/p>\n\n\n\n<p>Wait a minute. Let&#8217;s go back that code where we determine the border thickness.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component TextBox : FocusScope {\n    width: 200\n    height: textInput.implicitHeight + 8\n\n    Rectangle {\n        id: background\n        anchors.fill: parent\n        border {\n            <strong>\/\/ Border is thick and black,\n           \/\/ if textInput has focus=true<\/strong>\n            width: textInput.focus ? 2 : 1\n            color: textInput.focus ? \"black\" : \"gray\"\n        }\n\n        TextInput {\n            id: textInput\n            width: parent.width - 8\n            anchors.centerIn: parent\n            focus: true\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Clearly all the four <strong>TextInput<\/strong> instances within the four <strong>TextBox<\/strong> items have their <strong>focus<\/strong> property set to <strong>true<\/strong>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Look ma, many items seem to have focus!!!<\/h2>\n\n\n\n<p>Shouldn&#8217;t only one item have focus?<\/p>\n\n\n\n<p>Yes, only one item actually has keyboard focus. Really, pinky swear. Believe me. I am telling the truth. ?<\/p>\n\n\n\n<p>But within a <strong>FocusScope<\/strong>, its possible for an item can have its <strong>focus<\/strong> property set to <strong>true<\/strong>, without it actually having focus. ?<\/p>\n\n\n\n<p>Let&#8217;s demystify this. Take a look at this code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component ThickRectangle : Item {\n    width: 80; height: 80\n\n    Rectangle {\n        anchors.fill: parent\n        border { width: focus ? 4 : 1; color: \"black\" }\n        <strong>focus: true<\/strong> \n    }\n}\n\nGrid {\n    columns: 2\n    spacing: 5\n\n    ThickRectangle { }\n    ThickRectangle { }\n    ThickRectangle { }\n    ThickRectangle { }\n}<\/code><\/pre>\n\n\n\n<p>We are defining a component called <strong>ThickRectangle<\/strong> whose border width becomes &#8220;thick&#8221; when it has keyboard focus. We then place 4 of these within a <strong>Grid<\/strong> positioner. Notice that the <strong>Rectangle<\/strong> item within <strong>ThickRectangle<\/strong> component requests for focus upon creation. When we execute this program, we will see &#8230;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-5.png\" alt=\"\" class=\"wp-image-240\" width=\"320\" height=\"268\" srcset=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-5.png 640w, https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-5-300x251.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n<p>&#8230; only the first rectangle actually receiving focus. This means that the <strong>focus<\/strong> property value on other rectangles became <strong>false<\/strong>. We can now relax, QML engine is working fine. Only one of the items actually got focus and actually has <strong>focus<\/strong> property set to <strong>true<\/strong>, even though many had set their <strong>focus<\/strong> property to <strong>true<\/strong> in code. So far, so good. <\/p>\n\n\n\n<p>The QML engine assigns focus to the first item that requested for focus, which consequently causes <strong>focus<\/strong> on all other items to become <strong>false<\/strong>, even though it was initialised as true in code.<\/p>\n\n\n\n<p>Now, let&#8217;s change one small thing in the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component ThickRectangle : <strong>FocusScope<\/strong> {\n    width: 80; height: 80\n\n    Rectangle {\n        anchors.fill: parent\n        border { width: focus ? 4 : 1; color: \"black\" }\n        focus: true\n    }\n}\n\nGrid {\n    columns: 2\n    spacing: 5\n    anchors.centerIn: parent\n\n    ThickRectangle { }\n    ThickRectangle { }\n    ThickRectangle { <strong>focus: true<\/strong> }\n    ThickRectangle { }\n}<\/code><\/pre>\n\n\n\n<p>Notice that the root item of <strong>ThickRectangle<\/strong> component is now a <strong>FocusScope<\/strong>, instead of <strong>Item<\/strong>. And, we are setting <strong>focus<\/strong> of the third instance of <strong>ThickRectangle<\/strong> to <strong>true<\/strong>. This time, when we run the program we will see &#8230;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-6.png\" alt=\"\" class=\"wp-image-242\" width=\"320\" height=\"268\" srcset=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-6.png 640w, https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-6-300x251.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n<p>&#8230; all <strong>Rectangle<\/strong>s within <strong>ThickRectangle<\/strong> instances have their <strong>focus<\/strong> property set to <strong>true<\/strong>. But I promise, only the third item <em>really really really <\/em>has focus. <\/p>\n\n\n\n<pre class=\"wp-block-verse\">You see, the value of <strong>focus<\/strong> property in an item within a <strong>FocusScope<\/strong> is not used to signify which item has focus, but it is rather used to signify which item should get focus whenever the <strong>FocusScope<\/strong> receives focus.<\/pre>\n\n\n\n<p>For that reason, within a <strong>FocusScope<\/strong> it&#8217;s best to make use of <strong>Item.activeFocus<\/strong> to test if an item really has focus. <strong>Item.activeFocus<\/strong> is a read-only property, which will become true when an item has &#8220;active focus&#8221; (or real focus). This property is set by the underlying runtime and is always a dependable source of truth regarding keyboard focus, even within a <strong>FocusScope<\/strong>. So, let&#8217;s use this in addition to <strong>focus<\/strong> in our code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component ThickRectangle : FocusScope {\n    width: 80; height: 80\n\n    Rectangle {\n        anchors.fill: parent\n        border { width: focus ? 4 : 1; color: \"black\" }\n        <strong>color: activeFocus ? \"lightsteelblue\" : \"white\"<\/strong>\n        focus: true\n    }\n}\n\nGrid {\n    columns: 2\n    spacing: 5\n    anchors.centerIn: parent\n\n    ThickRectangle { }\n    ThickRectangle { }\n    ThickRectangle { focus: true }\n    ThickRectangle { }\n}<\/code><\/pre>\n\n\n\n<p>In this code, while border thickness is determined by <strong>focus<\/strong>, fill color is determined by <strong>activeFocus<\/strong>. Now, when we run the code we will see &#8230;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-7.png\" alt=\"\" class=\"wp-image-246\" width=\"320\" height=\"268\" srcset=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-7.png 640w, https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-7-300x251.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n<p>&#8230; all rectangles continue to have thick borders, but only the third item (the one with real focus) has lightsteelblue fill color. See, I told you so!<\/p>\n\n\n\n<p>So, moral of the story is: <span style=\"text-decoration: underline;\">within a <strong>FocusScope<\/strong>, always use <strong>activeFocus<\/strong> to determine who has real focus<\/span>.<\/p>\n\n\n\n<p>Okay, coming back to our TextBox example. Let&#8217;s use <strong>activeFocus<\/strong> to provide thick border on our <strong>Rectangle<\/strong> now.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component TextBox : FocusScope {\n    width: 200\n    height: textInput.implicitHeight + 8\n\n    Rectangle {\n        id: background\n        anchors.fill: parent\n        border {\n            width: textInput.<strong>activeFocus<\/strong> ? 2 : 1\n            color: textInput.<strong>activeFocus<\/strong> ? \"black\" : \"gray\"\n        }\n\n        TextInput {\n            id: textInput\n            width: parent.width - 8\n            anchors.centerIn: parent\n            focus: true\n        }\n    }\n}\n\nGrid {\n    columns: 2\n    spacing: 10\n    anchors.centerIn: parent\n\n    Text { text: \"Name: \" }\n    TextBox { }\n\n    Text { text: \"Place: \" }\n    TextBox { }\n\n    Text { text: \"Animal: \" }\n    TextBox { <strong>focus: true<\/strong> }\n\n    Text { text: \"Thing: \" }\n    TextBox { }\n}<\/code><\/pre>\n\n\n\n<p>Now, when I run the program I will see that only the TextBox I set focus to will have keyboard focus.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-8.png\" alt=\"\" class=\"wp-image-247\" width=\"320\" height=\"268\" srcset=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-8.png 640w, https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-8-300x251.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n<p>Now, we are getting somewhere. Isn&#8217;t it?<\/p>\n\n\n\n<p>In summary: <strong>FocusScope<\/strong> forwards keyboard focus to a specific child item within it. Said in other words, <strong>FocusScope<\/strong> behaves like a proxy for receiving keyboard focus into a specific child item. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What if we have FocusScope within FocusScope?<\/h2>\n\n\n\n<p>Consider this code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>component Form : FocusScope {\n    width: formLayout.width\n    height: formLayout.height\n\n    Grid {\n        id: formLayout\n        columns: 2\n        spacing: 10\n\n        Text { text: \"Name: \" }\n        TextBox { }\n\n        Text { text: \"Place: \" }\n        TextBox { }\n\n        Text { text: \"Animal: \" }\n        TextBox { focus: true }\n\n        Text { text: \"Thing: \" }\n        TextBox { }\n    }\n}\n\ncomponent TextBox : FocusScope {\n    width: 200\n    height: textInput.implicitHeight + 8\n\n    Rectangle {\n        id: background\n        anchors.fill: parent\n        border {\n            width: textInput.activeFocus ? 2 : 1\n            color: textInput.activeFocus ? \"black\" : \"gray\"\n        }\n\n        TextInput {\n            id: textInput\n            width: parent.width - 8\n            anchors.centerIn: parent\n            focus: true\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here, we notice<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TextBox<\/strong> is a <strong>FocusScope<\/strong> which forwards focus to the <strong>TextInput<\/strong> item inside it.<\/li>\n\n\n\n<li><strong>Form<\/strong> is a <strong>FocusScope<\/strong> which forwards focus to the third <strong>TextBox<\/strong> inside it.<\/li>\n<\/ul>\n\n\n\n<p>Now, when we create an instance of the Form item like this &#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Form { }<\/code><\/pre>\n\n\n\n<p>&#8230; none of the fields will actually get focus, because the <strong>Form<\/strong> item (which is a <strong>FocusScope<\/strong>) doesn&#8217;t have &#8220;real focus&#8221; (or <strong>activeFocus<\/strong>). However, the third <strong>TextBox<\/strong>&#8216;s <strong>focus<\/strong> property will be true and within that the <strong>TextInput<\/strong> item&#8217;s <strong>focus<\/strong> will be <strong>true<\/strong>. That&#8217;s okay, because like I said before the meaning of <strong>focus<\/strong> property within a <strong>FocusScope<\/strong> is different. It only signifies which item will receive focus, should the <strong>FocusScope<\/strong> actually get focus. If the <strong>FocusScope<\/strong> itself doesn&#8217;t have focus, then none of the items have focus anyway.<\/p>\n\n\n\n<p>But when I set <strong>focus<\/strong> on the <strong>Form<\/strong> &#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Form { focus: true }<\/code><\/pre>\n\n\n\n<p>&#8230; the <strong>FocusScope<\/strong> which the form is forwards focus to the third <strong>TextBox<\/strong>, which is another <strong>FocusScope<\/strong> that forwards focus to the <strong>TextInput<\/strong> within it. And we get exactly what we were hoping to get:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-8.png\" alt=\"\" class=\"wp-image-247\" width=\"320\" height=\"268\" srcset=\"https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-8.png 640w, https:\/\/www.vcreatelogic.com\/wp-content\/uploads\/2023\/07\/image-8-300x251.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/figure>\n\n\n\n<p><strong>FocusScope<\/strong> is a proxy for a child-item&#8217;s focus, and if that child-item is a <strong>FocusScope<\/strong> then it will proxy for its child-item and so on, until an actual item receives keyboard focus. Clear now?<\/p>\n\n\n\n<p>In all honesty, this wouldn&#8217;t have been so damn confusing if it was called <strong>FocusProxy<\/strong> instead of <strong>FocusScope<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you look at the documentation of FocusScope, you will see this: Focus scopes assist in keyboard focus handling when building reusable QML components. God I wish I could understand the entirety of FocusScope by reading this line of text. To be fair, the documentation has links to a rather elaborate article which explains how &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.vcreatelogic.com\/index.php\/2023\/07\/14\/what-exactly-does-focusscope-do-in-qml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;What exactly does FocusScope do in QML?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":54,"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/posts\/211\/revisions\/281"}],"wp:attachment":[{"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vcreatelogic.com\/index.php\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}