Fail to get active focus on ListView enclosed in FocusScope after changing it's visibility
-
When I set visibility of ListView enclosed in FocusScope to false and then return it to true, it fails to get active focus (changing it's focus property does no effect). If I use Item instead of FocusScope or change opacity instead of visibility - all works fine.
Is this a Qt bug or I misunderstood something? Qt 4.8.1, Windows 7.TestFocus.qml:
@import QtQuick 1.1Rectangle {
width: 360
height: 400ListModel { id: lm ListElement { data: "1" } ListElement { data: "2" } } FocusScope { focus: true width: parent.width height: parent.height - buttonBox.height ListView { id: view visible: false anchors.fill: parent spacing: 5 model: lm highlight: Rectangle { color: "red" } delegate: Rectangle { width: view.width height: 50 radius: 10 border { color: "gray" width: 3 } Text { anchors.centerIn: parent text: model.data } } onActiveFocusChanged: { console.log("activeFocusChanged", activeFocus) } onFocusChanged: { console.log("focusChanged", focus) } } Rectangle { id: dummy visible: false anchors.fill: parent color: "black" } } Rectangle { id: buttonBox width: parent.width height: 50 anchors.bottom: parent.bottom Button { height: parent.height width: parent.width / 2 buttonLabel: "ListView" anchors.left: parent.left onButtonClick: { dummy.focus = false dummy.visible = false view.visible = true view.focus = true } } Button { height: parent.height width: parent.width / 2 buttonLabel: "Dummy" anchors.right: parent.right onButtonClick: { view.focus = false view.visible = false dummy.visible = true dummy.focus = true } } }
}@
Button.qml:
@import QtQuick 1.1Rectangle {
property alias buttonLabel: label.text
signal buttonClick()
id: button
width: 50
height: 25
border {
width: 2
color: "gray"
}
Text {
id: label
anchors.centerIn: parent
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: buttonClick()
}
}@ -
I think you have to use forceActiveFocus() instead of just setting focus to true.
-
Yes, forceActiveFocus() works and I'm using it now. But I'm also wondering, if it is correct behavior to fail to get active focus after item become invisible.
[quote author="ludde" date="1346177044"]I think you have to use forceActiveFocus() instead of just setting focus to true.[/quote]
-
There are still some serious bugs in this Focus stuff from QML
Even the QML example about focus (declarative/keyinteraction/focus/) does not work.
I modified the program from above a little bit to show the issues@import QtQuick 1.1
Rectangle {
width: 360
height: 400ListModel { id: lm ListElement { data: "1" } ListElement { data: "2" } } FocusScope { id :focusScope focus: true width: parent.width height: parent.height - buttonBox.height onActiveFocusChanged: { console.log("FocusScope activeFocusChanged", focusScope.activeFocus) } onFocusChanged: { console.log("FocusScope focusChanged", focusScope.focus) } ListView { id: view visible: false anchors.fill: parent spacing: 5 model: lm highlight: Rectangle { color: "red" } delegate: Rectangle { width: view.width height: 50 radius: 10 border { color: "gray" width: 3 } Text { anchors.centerIn: parent text: model.data } } onActiveFocusChanged: { console.log("Listview activeFocusChanged", view.activeFocus) } onFocusChanged: { console.log("Listview focusChanged", view.focus) } } Rectangle { id: dummy visible: false anchors.fill: parent color: "black" onActiveFocusChanged: { console.log("Dummy activeFocusChanged", dummy.activeFocus) } onFocusChanged: { console.log("Dummy focusChanged", dummy.focus) } } } Rectangle { id: buttonBox width: parent.width height: 50 anchors.bottom: parent.bottom Button { height: parent.height width: parent.width / 2 buttonLabel: "ListView" anchors.left: parent.left onButtonClick: { //dummy.focus = false dummy.visible = false view.visible = true //view.focus = true view.forceActiveFocus() } } Button { height: parent.height width: parent.width / 2 buttonLabel: "Dummy" anchors.right: parent.right onButtonClick: { //view.focus = false view.visible = false dummy.visible = true //dummy.focus = true dummy.forceActiveFocus() } } }
}@
At the start of the program it outputs:
- FocusScope focusChanged true // ok logic
Now we click on the Listview button
- Listview activeFocusChanged false
- FocusScope focusChanged false
Now we click on the Dummy button
- Listview activeFocusChanged false
- FocusScope activeFocusChanged false
- Listview focusChanged false
- Dummy focusChanged true
- FocusScope activeFocusChanged false
- Dummy activeFocusChanged true
- Dummy focusChanged true
Why does the FocusScope changes the activeFocus property to false ???? According to the documentation it should be true.
What i'm seeing here is also the problem why the QML keyinteraction example does not work as expected. (Using Qt 4.8.2)
Doc says (about forceActiveFocus) : Forces active focus on the item.This method sets focus on the item and makes sure that all the focus scopes higher in the object hierarchy are also given the focus.
But it doesn't seem to be true. Bug ??
Wim.