Focus issue in SwipeView with Repeater
-
Hello,
once again I have no idea what is going on with the SwipeView.
There is already a forum post about focus problems with SwipeView and Loader, where forceActiveFocus() helped at the end. But that did not work for me.
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { width: 200 height: 200 visible: true title: qsTr("Hello World") Item { anchors.fill: parent SwipeView { id: swipe anchors.fill: parent focus: true Repeater { id: rep model: 3 Rectangle { id: rec1 property bool isCurrItm: ListView.isCurrentItem // (1) //property bool isCurrItm: SwipeView.isCurrentItem // (2) //property bool isCurrItm: index === 0 // (3) height: 100 width: 100 color: rec1.activeFocus ? "gray" : "green" Rectangle { id: rec2 height: 50 width: 50 focus: rec1.isCurrItm color: rec2.activeFocus ? "black" : "red" } } } } } }
Using (1)
property bool isCurrItm: ListView.isCurrentItem
works as expected:(2) and (3) don't:
Any ideas why the attached property of a ListView works?
-
Sorry to be confused.. I just want the inner rectangle (rec2) of the currentItem of the SwipeView to have activeFocus and therefore the color black.
-
Ok, to keep it simple and more understandable. I removed the Repeater and used a TextField instead of the internal Rectangle. Then I want the cursor to blink in the TextField when my component is displayed, so that the user can write something into the TextField immediately without having to click on it.
In my understanding the TextField should have the activeFocus after loading. But it does not.
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { id: window width: 200 height: 200 visible: true title: qsTr("Hello World") Item { anchors.fill: parent SwipeView { id: swipe anchors.fill: parent focus: true Rectangle { id: rec height: 100 width: 100 color: activeFocus ? "gray" : "green" TextField { id: txt focus: true text: activeFocus ? "focus" : "no focus" } } } } }
-
I need a FocusScope for the SwipeView items. Then everything works as intended.
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 Window { id: window width: 200 height: 200 visible: true title: qsTr("Hello World") Item { anchors.fill: parent SwipeView { id: swipe anchors.fill: parent focus: true FocusScope { id: scope focus: SwipeView.isCurrentItem Rectangle { anchors.fill: parent color: activeFocus ? "gray" : "green" TextField { focus: true text: activeFocus ? "focus" : "no focus" } } } } } }
Also adding the Repeater again works!
Thanks for listening..