2

我有一个名为 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


    }
}
4

1 回答 1

3

问题是当您查看下一行时,您的行list.currentIndex = index不以 then 结尾,因此解析器认为您正在尝试执行此操作:;(

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

所以它假设你正在调用index()不存在的函数。

只需替换你的两条线

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

通过这些

list.currentIndex = index;

我认为始终将;QML 函数(例如单击处理程序)中的语句放在语句的末尾是一个好习惯。其他人甚至将;每个财产声明放在最后以避免这些问题!

于 2013-08-21T09:47:51.120 回答