Qml 重叠自定义元素

问题描述 投票:0回答:1

我正在尝试了解锚点并在自定义元素中使用。这是一个简单的矩形。但我的问题是两个自定义矩形元素相互重叠。



//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 新手。

与完整描述相同

qt qml qt5
1个回答
0
投票

您应该为您的

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
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.