QML Dialog with focused textField

10,292

Solution 1

You don't need the function as it is written. From the docs of Dialog for the function open():

Shows the dialog to the user. It is equivalent to setting visible to true.

Given that (it's not the problem) it seems like the focus is continously contended between the dialog and the contained element. The more you open/close the Dialog, the more evaluations occurs. I cannot figure out why this happens, right now. However, you can easily solve the problem by (1) getting rid of onVisibilityChanged handler and (2) rewriting newFolder(). Final code rewritten:

ApplicationWindow {
    width: 360
    height: 300
    visible: true

    Button {
        anchors.centerIn: parent
        text: "click me!"
        onClicked: newFolder()
    }

    Dialog {
        id: newFolderDialog
        title: "New folder"
        height: 150
        width: 300
        standardButtons: StandardButton.Ok | StandardButton.Cancel
        focus: true    // Needed in 5.9+ or this code is NOT going to work!! 

        Column {
            anchors.fill: parent
            Text {
                text: "Name"
                height: 40
            }
            TextField {
                id: newFolderInput
                width: parent.width * 0.75
                focus: true
                onFocusChanged: console.log("Focus changed " + focus)
            }
        }
    }

    function newFolder() {
        newFolderDialog.open()
        newFolderInput.focus = true
    }
}

This way you first open the dialog and then set the focus to the proper Item.

Solution 2

I had to use forceActiveFocus() method after opening the dialog

onClicked: {
    dialog.open()
    input.forceActiveFocus()
}
Share:
10,292
user3412372
Author by

user3412372

Updated on July 25, 2022

Comments

  • user3412372
    user3412372 almost 2 years

    I am working on Qt quick application and I wanna open dialog. In this dialog window is TextField and I want to set focus to this textField after dialog is open. This code doesn't work.

    function newFolder() {
        newFolderDialog.visible = true
        newFolderDialog.open()
    }
    
    Dialog {
        id: newFolderDialog
        title: "New folder"
        height: 150
        width: 300
        standardButtons: StandardButton.Ok | StandardButton.Cancel
    
        Column {
            anchors.fill: parent
            Text {
                text: "Name"
                height: 40
            }
            TextField {
                id: newFolderInput
                width: parent.width * 0.75
                focus: true
                onFocusChanged: console.log("Focus changed " + focus)
            }
        }
    
        onVisibilityChanged: {
            if(visible === true){
                newFolderInput.text = ""
                newFolderInput.focus = true
            }
    
        }
    }
    

    output to console is:

    qml: Focus changed false
    qml: Focus changed true
    qml: Focus changed false

    It look like, that somehow focus is changed after I set focus to textField