xyygudu 2022-05-15 12:09
浏览 20
已结题

为什么QML ListView代理的第一项宽度为0

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的第一项的宽度又正常,这是为什么

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 5月23日
    • 修改了问题 5月15日
    • 创建了问题 5月15日

    悬赏问题

    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据