Why parent.height doesn't work



  • Hi, I have a simple trouble about using parent information. I wonder under what scenarios does it works. Below is my code snippet:

    Page {
    
        id: mainPage
    
        height: Screen.desktopAvailableHeight - 55 - 30
        width: Screen.desktopAvailableHeight * 1.1
    
        ColumnLayout {
    
            anchors.fill: parent
            spacing: parent.height * 0.1    // DOESN'T WORK
    
            Text {
    
                text: qsTr("请选择下列一项功能:")
    
                font.pointSize: 16
    
                Layout.leftMargin: mainPage.width * 0.05     // DOESN'T WORK
            }
    
            ........
    }
    

    Both the "spacing: parent.height * 0.1" and "Layout.leftMargin: mainPage.width * 0.05" are 0 !! However, in the Qt Creator Design mode, both of them are correct.
    Can anyone give me some suggestions? Or actually I didn't use a recommended form to refer to parent information? Thanks



  • Try this:

    spacing: Math.floor(parent.height * 0.1)
    

    Edit: Also, you probably should not specify anchor points for ColumnLayout.



  • @z3dd said in Why parent.height doesn't work:

    Edit: Also, you probably should not specify anchor points for ColumnLayout.

    Why not ? That's the point of layouts, you specify a size and it will spread its children to fill it. If you don't want that, use a Column.



  • @GrecKo Oh yes, you right! I must have mixed up Column and ColumnLayout.



  • Try using parent.implicitHeight. I've found that this sometimes works when height doesn't work as expected


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.