简体   繁体   中英

How to set focus to TextField inside ListView delegate?

I have ListView with some text input fields inside it:

Window {
    visible: true

    ListModel {
        id: textModel
        ListElement {
            text: "Bill Smith"
        }
        ListElement {
            text: "John Brown"
        }
        ListElement {
            text: "Sam Wise"
        }
    }

    ListView {
        width: 180; height: 200
        focus: true

        model: textModel
        delegate: RowLayout{
            id: layout
            Label {
                text: model.text
            }
            TextField {
                text: model.text
            }
        }
    }
}

And I want to set input focus to the first TextField in the list. How can I do this? If I add focus: true in ListView it does not help.

You have to activate the focus of the TextField using the ListView.isCurrentItem property:

ListView {
    id: view
    width: 180; height: 200
    focus: true
    model: textModel
    delegate: RowLayout{
        id: layout
        Label {
            text: model.text
        }
        TextField {
            
            text: model.text
        }
    }
    // Component.onCompleted: view.currentIndex = 0
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM