listview 的delegate的宽度设置为listview的宽度后,为什么第一项宽度为0,从第二项开始才是listview的宽度,而且组件初始化顺序为什么是: delegate->row->listRectID
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
id: root
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Row{
id: row
anchors.fill: parent
Component.onCompleted: {
console.log("row", width)
}
Rectangle{
id: listRectID
width: parent.width
height: parent.height
Component.onCompleted: {
console.log("listRectID", width)
}
ListView{
anchors.fill: parent
model: 2
delegate: Rectangle{
width: ListView.view.width
height: 30
color: index === 0 ?"#465":"#e45"
Component.onCompleted: {
/*不知道为什么,代理的第一项宽度为0,第二项才是listview的宽度
但窗口显示又没有问题*/
console.log("delegate", width)
}
}
}
}
}
}
运行结果:
qml: delegate 0
qml: row 640
qml: listRectID 640
qml: delegate 640
如果把row的anchors.fill: parent注释,换成并添加:width: 200 ;height: 200,此时delegate的第一项的宽度又正常,这是为什么