如何知道" y"在ListView中创建Item时的属性

时间:2017-05-16 06:32:17

标签: qt listview qml qtquick2

我使用ListView我需要填充我的窗口,其中插入了最后一个Item 在这种情况下,我设置了ListView属性:contentY,其y值为Item

我的问题是,当您插入新项目时,我不知道如何知道项目的y值,因为它尚未设置

这是我尝试的:

ScrollView {
    id: scroll
    ListView {
        model: DelegateModel {
            id: visualModel
            model: myModel //Model is set in the cpp
            delegate: Rectangle {
                id: rectangleDelegate

                Component.onCompleted: {
                   rectangleDelegate.ListView.view.contentY = rectangleDelegate.y
                   console.log(rectangleDelegate.y ) //Not set yet (y = 0)
                }

                Button {
                    onClicked { 
                        rectangleDelegate.ListView.view.contentY = rectangleDelegate.y
                        console.log(rectangleDelegate.y ) //Now it is ok (y = rightValue)
                    }
                }
            }
        }
    }
}   

我该怎么办?

1 个答案:

答案 0 :(得分:0)

感谢@derM,这是一个有效的简单示例:

ScrollView {
    id: scroll
    ListView {
        model: DelegateModel {
            id: visualModel
            model: myModel //Model is set in the cpp
            delegate: Rectangle {
                id: rectangleDelegate

                onYchanged: {
                   rectangleDelegate.ListView.view.contentY = rectangleDelegate.y
                }
            }
        }
    }
}