0

我有关于 qml 的代码,它应该通过点击将它分成 4 个正方形。

import QtQuick 2.0

Rectangle {
    Component {
        id: squareComponent
        Rectangle {
            property int sideLenght: 500
            width: sideLenght
            height: sideLenght
            color: "orange"
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    var first = squareComponent.createObject(parent)
                    var second = squareComponent.createObject(parent)
                    var third = squareComponent.createObject(parent)
                    var fourth = squareComponent.createObject(parent)

                    var sideLenght = parent.sideLenght / 2

                    first.sideLenght = sideLenght
                    second.sideLenght = sideLenght
                    third.sideLenght = sideLenght
                    fourth.sideLenght = sideLenght

                    var x = parent.x
                    var y = parent.y
                    console.log(x, y)

                    first.x = x
                    first.y = y
                    first.color = "red"
                    console.log("first", first.x, first.y)

                    second.x = first.x + sideLenght
                    second.y = first.y
                    second.color = "orange"
                    console.log("second", second.x, second.y)

                    third.x = first.x
                    third.y = first.y + sideLenght
                    third.color = "blue"
                    console.log("third", third.x, third.y)

                    fourth.x = first.x + sideLenght
                    fourth.y = first.y + sideLenght
                    fourth.color = "black"
                    console.log("fourth", fourth.x, fourth.y, "\n\n")

                    parent.sideLenght = 0
                }
            }
        }
    }

    Component.onCompleted: squareComponent.createObject(parent)
}

它们被划分,但只划分正确的平方 (0, 0),其他的则与 x 或 y 的偏移量为父项的数量。Qt 5.0.1。我该如何解决这种行为,这是一个错误吗?

记录表明这些元素是正确的,但实际上并非如此。

4

1 回答 1

0

这就是我得到的输出QtQuick 1.1。这在 Qt 5.0.1 中有什么不同吗?

输出

于 2013-02-21T15:20:39.817 回答