Search code examples
qtqmlqtquickcontrols2

Grid does not respect leftMargin


Qt 5.11. I use Column to position my controls. For this column element I set anchors.leftMargin: 10. All children items respects this except Grid.

Screenshot of what I get:

enter image description here

QML code:

import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Column
    {
        anchors.fill: parent
        anchors.leftMargin: 10

        RadioButton
        {
            text: qsTr("System proxy")
        }

        RadioButton
        {
            text: qsTr("No proxy")
        }

        RadioButton
        {
            text: qsTr("Configure manually:")
        }

        Grid
        {
            columns: 5
            spacing: 10
            Label {text: " "}
            Label {text: qsTr("Address")}
            Label {text: qsTr("Port")}
            Label {text: qsTr("Login")}
            Label {text: qsTr("Password")}
            Label {text: "HTTP"}
            TextField {width: 100}
            TextField {width: 40}
            TextField {width: 100}
            TextField {width: 100}
        }
    }
}

Am I doing something wrong?


Solution

  • The problem is not the Grid but RadioButton, they have an additional padding:

    enter image description here

    A Control has the following layout:

    enter image description here

    So the solution set the leftPadding to 0 of the RadioButtons:

    import QtQuick 2.10
    import QtQuick.Controls 2.3
    import QtQuick.Layouts 1.3
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Column
        {
            anchors.fill: parent
            anchors.leftMargin: 10
    
            RadioButton
            {
                text: qsTr("System proxy")
                leftPadding: 0
            }
    
            RadioButton
            {
                text: qsTr("No proxy")
                leftPadding: 0
            }
    
            RadioButton
            {
                text: qsTr("Configure manually:")
                leftPadding: 0
            }
    
            Grid
            {
                columns: 5
                spacing: 10
                Label {text: " "}
                Label {text: qsTr("Address")}
                Label {text: qsTr("Port")}
                Label {text: qsTr("Login")}
                Label {text: qsTr("Password")}
                Label {text: "HTTP"}
                TextField {width: 100}
                TextField {width: 40}
                TextField {width: 100}
                TextField {width: 100}
            }
        }
    }
    

    enter image description here