简体   繁体   English

QML堆栈的ColumnLayout子项彼此重叠吗?

[英]QML stack ColumnLayout children on top of each other?

I'm new to QML, so I may be asking something obvious. 我是QML的新手,所以我可能会问一些明显的问题。 I'm struggling to stack elements in ColumnLayout on top of each other. 我正在努力将ColumnLayout中的元素彼此堆叠。 There's just too much spacing between them I can't do anything about. 它们之间的间距太大,我无能为力。 See screenshot for more details: 请参阅屏幕截图以获取更多详细信息:

在此处输入图片说明

Here's the actual QML: 这是实际的QML:

ColumnLayout {
    anchors.fill: parent
    spacing: 0

    Rectangle {
        color: "blue"
        Layout.fillWidth: true
        height: 50
    }

    Rectangle {
        color: "red"
        Layout.fillWidth: true
        height: 50
    }

    Rectangle {
        color: "yellow"
        Layout.fillWidth: true
        height: 50
    }
}

What I'm trying to do is to have the rectangles aligned top to bottom and have spacing between them maybe 2-3 pixels. 我正在尝试做的是使矩形从上到下对齐,并且它们之间的间距可能为2-3像素。

Thanks 谢谢

Your code will works fine if you remove the filling to parent and place there filling just to width 如果您将填充物移除到父级并将填充物放置到宽度上,则您的代码将正常工作

anchors.left: parent.left
anchors.right: parent.right 

After that the code will looks like this 之后,代码将如下所示

ColumnLayout {
    anchors.left: parent.left
    anchors.right: parent.right
    spacing: 2 //spacing between items in layout
    Rectangle {
        color: "blue"
        Layout.fillWidth: true
        height: 50
    }
    Rectangle {
    ...
    }
    Rectangle {
    ...
    }
}

By doing this, you will give to ColumnLayout rights to manage himself's height . 这样,您将授予ColumnLayout权限来管理自己的height Its height will related to its children's height and will not be stretched to the parent. 它的高度将与孩子的身高相关,并且不会拉伸到父级。

Another way is to manage items through anchor 另一种方法是通过anchor管理项目

Item {
  id: item1
}
Item {
  id: item2
  anchor.top: item1.bottom
  anchor.topMargin: 2 
}
Item {
  id: item3
  anchor.top: item2.bottom
  anchor.topMargin: 3 
}

Layouts good thing if you don't need to manage positions of items in specific way inside layout. 如果您不需要在布局中以特定方式管理项目的位置,则布局是一件好事。

Also it would be helpful to you to check this Use Case - Positioners and Layouts In QML 另外,检查该用例-QML中的定位器和布局也将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM