Qt 相同属性的不同值

Qt 相同属性的不同值,qt,qml,Qt,Qml," 在上面的代码中,即使someOtherRectId.x大于200ex 300,rightMargin也会得到值0。我还使用日志语句进行了检查。 为什么会观察到这种行为?您试图在同一部件中使用两个定位器系统,但您不能这样做 有第四种方法可以定位项目: 手动定位:带x和y。 锚点:使用anchor.left、anchor.centerIn。。。 布局:使用RowLayout、ColumnLayout。。。 带行、列、流的定位器。。。 对于解决方案,请使用以下代码: Item { Rectang

"

在上面的代码中,即使someOtherRectId.x大于200ex 300,rightMargin也会得到值0。我还使用日志语句进行了检查。
为什么会观察到这种行为?

您试图在同一部件中使用两个定位器系统,但您不能这样做

有第四种方法可以定位项目:

手动定位:带x和y。 锚点:使用anchor.left、anchor.centerIn。。。 布局:使用RowLayout、ColumnLayout。。。 带行、列、流的定位器。。。 对于解决方案,请使用以下代码:

Item {
 Rectangle {

 height: 100;
 width: 100;
 color: "red"
 anchors.right: someOtherRectId.left;
 anchors.rightMargin: someOtherRectId.x > 200 ? 100: 0;

 Component.onCompleted: {
        console.log("Other Rectangle X co-ordinate: ", someOtherRectId.x)
  } 
 }
 Rectangle {
  id: someOtherRectId
  height: 100;
  width: 100
  color: "gold"
  anchors.centerIn: parent
  }
}
有关定位系统的更多详细信息

见文件: 本视频:
请提供,至少另一个矩形可能你的问题是你有一个打字错误。anchors.centreIn应该是anchors.centreIn。我运行了您的代码,它给出了其他一些rectid.x作为-50。根项目没有指定宽度/高度,因此居中会给出负x值。你确定你的代码没有遗漏什么吗?因为是公司代码,我做了一些更改并上传了上面的代码。代码没有遗漏任何内容,因为我已经复制了它。感谢您的回复:。两个矩形都在窗口上绘制。但我的问题是anchors.rightMargin中的表达式总是求值为false,即使它应该为true。我还使用控制台检查了其他rectid.x值。这是不正确的。混合放置物品的方法是可以的,除非你做得不理智。混合高度/宽度和锚固件。右侧可以。高度/宽度和锚定点相同。但是设置x将被锚覆盖。对,OP的帖子中不是这样的。对于定位器,您可以在由行管理的项中使用anchors.top,但不能使用anchors.right。
    Item {
    Rectangle {
        height: 100
        width: 100
        color: "red"
        anchors.right: someOtherRectId.left
        anchors.rightMargin: someOtherRectId.x > 200 ? 100: 0

        Component.onCompleted: {
            console.log("Other Rectangle X co-ordinate: ", someOtherRectId.x)
        }
    }

    Rectangle {
        id: someOtherRectId
        
        x: parent.x + (width / 2)
        y: parent.y + (height / 2)
        height: 100
        width: 100
        color: "gold"
    }
}