1
votes

I do not understand why it does not work properly. I want to set TextEdit focus to true when the window pops out. TextEdit can receive key events only after a click has been performed on its area.

main.qml

ApplicationWindow {
    id:aplWin
    visible: true
    minimumWidth:  1280
    minimumHeight: 1024

    StackView {
        id: stackView
        anchors.fill: parent
        initialItem: SignInWin {}
    }
}

SignInWin.qml

Page {
    id: root
    width: parent.width + 500
    height: parent.height

    Rectangle {
        border.color: "black"
        y: 200
        width: 50
        height: 20
        z: 1
        TextEdit {
            anchors.fill: parent
            color: "black"
            focus: true
        }
    }
}
1
Try to forceActiveFocus() when Component.onComplete - derM

1 Answers

1
votes

The problems are those:

You have multiple layers in your focus tree. Each FocusScope can have one child that has focus. So now you have:

StackView -> Page -> TextEdit

What puzzles me, is that Page acts as if it were a FocusScope, but is not documented that way.

This means, that TextEdit won't have activeFocus unless the Page AND the StackView have. But activeFocus is required to retrieve the input.

So, you can either use the method forceActiveFocus() which will iterate over the focus hierarchy, and requesting the focus for each FocusScope, or you set the focus-property to true for each layer in the hierarchy.

ApplicationWindow {
    id:aplWin
    visible: true
    minimumWidth:  1280
    minimumHeight: 1024

    StackView {
        id: stackView
        anchors.fill: parent
        initialItem: SignInWin {}
        focus: true // <--- HERE!
    }
}

Page {
    id: root
    width: parent.width + 500
    height: parent.height
    focus: true // <--- AND HERE

    Rectangle {
        border.color: "black"
        y: 200
        width: 50
        height: 20
        z: 1
        TextEdit {
            anchors.fill: parent
            color: "black"
            focus: true
        }
    }
}