Cannot retreive focus in TextEdit inside a StackView item.



  • Hello! 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
            }
        }
    }
    


  • Getting focus to work correctly has always been difficult for me, but I think in this case you also need to set the focus of your StackView to true as well. I just tested it, you also need to set the focus of the Page is SignInWin to true. Then it will work as you want it to.

    The problem is I don't really know why it works this way. I would need to read more documentation or ask someone who really understand how focus works to really understand this.



  • @stcorp my man awesome! thank you



  • you can try this example in SignInWin.qml :

    Page {
        id: root
        width: parent.width + 500
        height: parent.height
        focus: true
    
        Rectangle {
            border.color: "black"
            y: 200
            width: 50
            height: 20
            z: 1
            TextEdit {
                id:txt
                anchors.fill: parent
                color: "black"
                focus: true
            }
        }
        Component.onCompleted:{
    
            txt.forceActiveFocus()
        }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.