Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why is item width 0 when using anchors.fill: parent?



  • Hello forum members, I'm strugglin to create a dynamic row component. It seems to be related to the problem that it's width is 0, although it spans the whole window.
    Minimal example code using a rectangle instead of a row below:

    Window {
        id: testWin
        title: "WDC Test View"
    
        Rectangle {
            border.color: "red"
            border.width: 2
            color: "orange"
            anchors.fill: parent
            Component.onCompleted: console.log(width)
        }
    }
    

    Looks like this on my computer:
    ![picture](0_1534535931240_Screenshot_20180817_215816.png
    Console output is "0". But i would expect the window width.
    Why isn't the width set to the rectangles real width?
    How could I get the real width?



  • Answering my own post. It seems that the item width is 0 onComplete and width changed afterwards. So following code will output the window width:

    Window {
        id: testWin
        title: "WDC Test View"
    
        Rectangle {
            border.color: "red"
            border.width: 2
            color: "orange"
            anchors.fill: parent
            onWidthChanged: console.log(width)
        }
    }
    

Log in to reply