Qml 焦点未转移到Listview中单击的代理

Qml 焦点未转移到Listview中单击的代理,qml,Qml,我有一个名为AddressModel的ListModel和一个显示模型内容的Listview,委托包含两个矩形组件(用作向上和向下按钮)和一个文本组件,我的要求是将焦点转移到单击的委托。我在委托中添加了行list.currentIndex=index,但它不起作用。请帮我解决这个问题 这是我的ListView.Qml文件 import QtQuick 2.0 Rectangle { id: idRect width: 600 ; height: 300 Compon

我有一个名为AddressModel的ListModel和一个显示模型内容的Listview,委托包含两个矩形组件(用作向上和向下按钮)和一个文本组件,我的要求是将焦点转移到单击的委托。我在委托中添加了行
list.currentIndex=index
,但它不起作用。请帮我解决这个问题

这是我的ListView.Qml文件

import QtQuick 2.0

Rectangle {
    id: idRect

    width: 600 ; height: 300

    Component {
        id: idCompDelegate
        Item {
            id: idItmRow
            width: 600
            height: 50
            Row {
                id: idRow

                spacing: 100

                Rectangle {
                    id: idRectUp

                    width: 150
                    height: 50
                    color: "lightsteelblue"
                    radius: 8
                    Text {
                        anchors.centerIn: parent
                        text: "Up"
                    }

                    MouseArea {
                        id: idMouseAreaUp
                        anchors.fill:  parent
                        propagateComposedEvents: true
                        onClicked: {

                            list.currentIndex = index   //it's not working
                                    ((list.currentIndex)===0) ? console.log("Cannot Move towards Up"):list.model.move(list.currentIndex,list.currentIndex-1,1)


                        }
                    }
                }

                Text {
                    id: idCenterText
                    width: 100
                    text: firstName+" "+lastName
                }
                Rectangle {
                    id: idRectDown

                    width: 150
                    height: 50
                    color: "lightsteelblue"
                    radius: 8
                    Text {
                        anchors.centerIn: parent
                        text: "Down"
                    }
                    MouseArea {
                        id: idMouseAreaDown
                        anchors.fill:  parent
                        onClicked: {
                              list.currentIndex = index //it's not working
                            (list.count === (list.currentIndex)+1)? console.log("Cannot Move towards Down"):list.model.move(list.currentIndex,list.currentIndex+1,1)
                        }
                    }
                }
            }


        }


    }


    ListView {
        id: list
        width: parent.width
        height: parent.height

        model: AddressModel {}

        delegate:  idCompDelegate
        highlight: Rectangle { color: "lightgrey" ; radius : 8 }
        highlightFollowsCurrentItem: true
        focus: true

        spacing: 5


    }
}

问题是您的行
list.currentIndex=index
没有以
结尾然后,当您查看下一行时,它以
开始,因此解析器认为您正在尝试这样做:

list.currentIndex = index((list.count === (list.currentIndex)+1)
因此,它假设您正在调用不存在的函数
index()

把你的两条线都换掉

list.currentIndex = index //it's not working
通过这些

list.currentIndex = index;
我认为在QML函数中总是将
放在语句的末尾是一种很好的做法,例如在单击处理程序中。其他人甚至在每个属性声明的末尾放
,以避免这些问题