焦点不会转移到Listview中单击的委托

时间:2013-08-21 05:23:20

标签: qml

我有一个名为AddressModel的ListModel和一个Listview来显示模型内容,Delegate包含两个矩形组件(充当向上和向下按钮)和一个Text组件,我的要求是将焦点转移到已点击的委托我添加了委托中的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


    }
}

1 个答案:

答案 0 :(得分:2)

问题是,您的第list.currentIndex = index行不会以;结尾,那么当您查看下一行时,它会以(开头,因此解析器会认为您正在尝试这个:

list.currentIndex = index((list.count === (list.currentIndex)+1)

因此它假定您正在调用不存在的函数index()

只需更换两行

即可
list.currentIndex = index //it's not working

通过这些

list.currentIndex = index;

我认为最好将;放在QML函数的语句末尾,例如点击处理程序。其他人甚至在每个财产声明的末尾加上;以避免这些问题!