QML - Not able to get width,height etc in “On Completed”

前端 未结 2 1327
鱼传尺愫
鱼传尺愫 2021-01-16 11:25

I need to get width and height of a Rectangle in Component.OnCompleted handler but if i print the same i am getting some unknown values, Follow

2条回答
  •  轮回少年
    2021-01-16 11:30

    The parent of items, nested directly in the window is not the window, but the window's contentItem.

    Try this instead:

    Rectangle{
           id:rect
           width: appWindow.width * 0.75
           height: appWindow.height * 0.70
    }
    

    The same applies for your "full code":

    Rectangle{
            id:rectParent
            width:appWindow.width * 0.75
            height: appWindow.height * 0.70
            Rectangle{
                id:rectChild
                width:parent.width * 0.75
                height: parent.height * 0.70
                Component.onCompleted: {
                    console.log("Width=",width) //prints "Width= 337.5"
                }
            }
        }
    

    You can use parent in the second rectangle, because its parent will be the first rectangle, but since the first is nested inside the window, it needs to refer to the window instead of its parent to get property sized.

提交回复
热议问题