我正在尝试了解锚点并在自定义元素中使用。这是一个简单的矩形。但我的问题是两个自定义矩形元素相互重叠。
//CRect.qml
import QtQuick 2.12
Item{
property var bgColor:"red"
Rectangle {
width:100
height:100
color:bgColor}}
//main.qml
...
Rectangle{
id: containerId
width:300
height:width
border.color:"black"
anchors.centerIn: parent
CRect{
id: topLeftRectId
}
CRect{
id:topCenterRectId
bgColor:"blue"
anchors.left: topLeftRectId.right}}}
出了什么问题?我是 qml 新手。
与完整描述相同
您应该为您的
CRect
中的商品指定尺寸。否则,锚点将作用于宽度和高度为 0 的项目,并且对齐接缝会出现错误,因为内部矩形与其父级重叠。
试试这个:
Item {
property color bgColor: "red"
// size the parent
width:100
height:100
Rectangle {
// give your rectangle the parent size
anchors.fill: parent
color:bgColor
}
}