为什么在更改可见值时会更改QML x,y坐标?

时间:2014-06-10 11:08:32

标签: qt qml

我在QML上遇到了问题。

制作这样的矩形

Rectangle {
   id: rect1
   x: 100
   y: 100
   width: 100
   height: 100
   Rectangle {
      id: rect2
      x: 50
      y: 50
      width: 50
      height: 50
   }
}

然后更改rect1的可见值。 但有时x,y坐标变为x = 0,y = 0的rect2 !!!

当我将其从可见变为不透明时,它的工作正常。

我认为这可能是一个错误...是不是? 我该如何解决......

1 个答案:

答案 0 :(得分:1)

坐标对我来说没有变化:

import QtQuick 2.0
import QtQuick.Controls 1.1

Item {
    width: 400
    height: 400

    Rectangle {
       id: rect1
       x: 100
       y: 100
       width: 100
       height: 100
       color: "red"

       Rectangle {
          id: rect2
          x: 50
          y: 50
          width: 50
          height: 50
          color: "blue"
       }
    }

    Column {
        Text {
            text: "rect1 x/y/width/height: " + rect1.x + " " + rect1.y + " " + rect1.width + " " + rect1.height
        }
        Text {
            text: "rect2 x/y/width/height: " + rect2.x + " " + rect2.y + " " + rect2.width + " " + rect2.height
        }
        Button {
            text: rect1.visible ? "Hide rect1" : "Show rect1"
            onClicked: rect1.visible = !rect1.visible
        }
    }
}