Jacob Krieg
Jacob Krieg

Reputation: 3164

Why I can't anchor QML Items to one another

I have this code:

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"
        }
    }
}

I want to anchor the two rects to one another so the following output is generated:

enter image description here

Why doesn't my code do so? I would prefer not to use layouts...

Upvotes: 0

Views: 634

Answers (1)

dtech
dtech

Reputation: 49279

The problem is that the left rectangle's right anchor is set to the right rectangle's left anchor, which is set to the left rectangle's right anchor.

So you have a var a = b = a scenario, with no actual, concrete value being used.

Try setting the left rectangle's width to parent.width * .5 and only anchoring the right rectangle to it.

You have to have some concrete value in order to use values that are relative to it. In this case none of the rectangles has a valid width, so it is unable to determine the position of the edge they are sharing.

Upvotes: 3

Related Questions