如何更改列或行中项目之间的间距

Jeg*_*ggu 5 qml qt5 qtquick2

我正在调整Item使用ColumnRow键入QML.有没有办法给每个人留出不同的间距Item.以下内容:

喜欢:

ITEM1

间距:10

ITEM2

间距:20

项目3

间距:40

ITEM4

这是我的代码:

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

    Rectangle{
        id: rect
        anchors.fill: parent

        Column{
            id: column
            anchors.centerIn: parent
            spacing: 10

            Row{
                id: row1
                anchors.horizontalCenter:  parent.horizontalCenter
                Rectangle{
                    width: 300
                    height: 100
                    color: "lightgreen"
                }
            }
            Row{
                id: row2
                anchors.horizontalCenter:  parent.horizontalCenter
                Rectangle{
                    width: 100
                    height: 50
                    color: "lightblue"
                }
            }
            Row{
                id: row3
                anchors.horizontalCenter:  parent.horizontalCenter
                Rectangle{
                    width: 50
                    height: 50
                    color: "green"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

yau*_*yau 5

带有间隔项的 hacky 版本

有时我更喜欢这个而不是 ColumnLayout 因为在某些情况下我不能使用 ColumnLayout (虽然目前无法确切解释原因)。我得到它的工作如下

Column {
    Rectangle {
        // ...
    }

    Item {
        width: 1 // dummy value != 0
        height: 10
    }

    Rectangle {
        // ...
    }

    Item {
        width: 1 // dummy value != 0
        height: 20
    }

    Rectangle {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

宽度为 0 的项目将不起作用。我建议将 Spacer_Col.qml(和 Spacer_Row 模拟)放入你的工具箱中,看起来像

import QtQuick 2.8
Item {
    id: root
    property alias spacing: root.height
    width: 1 // dummy value different from 0
}
Run Code Online (Sandbox Code Playgroud)

使用列布局

ColumnLayout {
    Rectangle{
        // ...
    }

    Rectangle{
        Layout.topMargin: 10
        // ...
    }

    Rectangle{
        Layout.topMargin: 20
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)