为什么我不能将 QML 项目相互锚定

Why I can't anchor QML Items to one another

我有这个代码:

import QtQuick 2.7
import QtQuick.Window 2.2

Window {
    visible: true

    width: 640
    height: 480

    title: qsTr("Hello World")

    Rectangle {
        anchors.fill: parent

        Rectangle {
            id: rect1

            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: rect2.left
            anchors.bottom: parent.bottom
            color: "red"
        }

        Rectangle {
            id: rect2

            anchors.top: parent.top
            anchors.left: rect1.right
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            color: "blue"
        }
    }
}

我想将两个矩形相互锚定,以便生成以下输出:

为什么我的代码不这样做?我不想使用布局...

问题是左矩形的右锚点设置为右矩形的左锚点,右锚点设置为左矩形的右锚点。

所以你有一个 var a = b = a 场景,没有使用实际的具体值。

尝试将左侧矩形的宽度设置为 parent.width * .5 并仅将右侧矩形固定到它。

你必须有一些具体的值才能使用与之相关的值。在这种情况下,none 个矩形具有有效宽度,因此无法确定它们共享的边的位置。