[Qt-qml] FocusScope gives focus to the last item with focus: true

Girish Ramakrishnan girish at forwardbias.in
Thu Dec 16 17:03:48 CET 2010


Hi,
Per the docs (http://doc.qt.nokia.com/4.7-snapshot/qdeclarativefocus.html#qmlfocus),
"Within each focus scope one element may have Item::focus set to true.
If more than one Item has the Item::focus property set, the first is
selected and the others are unset, just like when there are no focus
scopes.". In the code below, rect2 gets focus which appears to
contradict what the docs say.

Also, the 'non-working' example in the documentation, works just fine
(i.e the MyWidget example that shows the expanded QML code on the
right without using FocusScope). And this appears to be because the
last 'focus' item got focus.

I am using 4.7.1.

Girish

FocusScope {
    id: rect
    width: 100
    height: 100
    focus: true

    Rectangle {
        id: rect1
        focus: true
        anchors.fill: parent
        Keys.onPressed: {
            console.log('a key was pressed in rect1')
            }
    }

    Rectangle {
        id: rect2
        focus: true
        anchors.fill: parent
        Keys.onPressed: {
            console.log('a key was pressed in rect2')
            }
    }
}


More information about the Qt-qml mailing list