Qt 快速版面边距

Qt 快速版面边距,qt,layout,qml,qtquick2,qt-quick,Qt,Layout,Qml,Qtquick2,Qt Quick,有没有办法删除QML布局组件中的起始页边距和结束页边距 下面是一个ColumnLayout的示例,其中有几个孩子。问题是如何删除这些顶部和底部边距,并沿父布局的整个高度重新分布所有子级 您不能将布局内的项目上下对齐。作为一种解决方法,您可以将带有变量y的项目放入容器中,如下所示: Window { visible: true visibility: Window.Maximized ColumnLayout { anchors.horizontalCe

有没有办法删除QML布局组件中的起始页边距和结束页边距

下面是一个ColumnLayout的示例,其中有几个孩子。问题是如何删除这些顶部和底部边距,并沿父布局的整个高度重新分布所有子级


您不能将布局内的项目上下对齐。作为一种解决方法,您可以将带有变量
y
的项目放入容器中,如下所示:

Window {
    visible: true
    visibility: Window.Maximized

    ColumnLayout {
        anchors.horizontalCenter: parent.horizontalCenter
        height: parent.height
        spacing:1
        Repeater {
            id: repeater
            model: 10
            Rectangle {
                color: "green"
                property int radius: 15
                width: radius
                Layout.fillHeight: true
                Rectangle {
                    anchors.horizontalCenter: parent.horizontalCenter
                    y: index * (parent.height - height) / (repeater.count - 1)
                    radius: parent.radius
                    width: radius * 2
                    height: radius * 2
                    color: "blue"
                }
            }
        }
    }
}

经过两次布局定位实验,我得出了以下解决方案

假设我要消除的边距的大小为布局子项之间间距的一半,我们可以将负边距设置为布局组件的开始和结束,将其拉伸,直到第一个子项和最后一个子项在布局的开始/结束处对齐

我用来计算利润的函数:

function getMargin() {
  var height = root.height;
  var spacing = (height - root.radius * 2 * model.length) / model.length;
  return spacing / 2;
}
如果
是布局组件的父级,
根.radius*2
表示布局子项的大小,可以替换为另一个子维度,
模型。长度
表示子项计数

然后,我们可以为布局组件设置锚定并设置相应的边距

ColumnLayout {
    anchors.top: root.top
    anchors.bottom: root.bottom
    anchors.topMargin: -1 * getMargin()
    anchors.bottomMargin: -1 * getMargin()
    Repeater {
        model: root.model
        Item {
            property int radius: root.radius
            width: radius * 2
            height: radius * 2
            /* more code */
        }
    }
}

另外,同样的解决方案也可以应用于RowLayout,方法是用左/右替换布局的上/下锚。

好的,你必须至少向我们展示你的代码,这样我们才能帮助你。@folibis my bad。代码已附加。您的代码不完整,因此我只能假设如何解决此问题。请尝试将Layout.alignment:Qt.AlignTop添加到activeDot\u容器中item@folibis但这样我只能在列的底部获得更大的边距。。。
ColumnLayout {
    anchors.top: root.top
    anchors.bottom: root.bottom
    anchors.topMargin: -1 * getMargin()
    anchors.bottomMargin: -1 * getMargin()
    Repeater {
        model: root.model
        Item {
            property int radius: root.radius
            width: radius * 2
            height: radius * 2
            /* more code */
        }
    }
}