Changing rectangle position that is depending to another rectangle position



  • Hi everyone. in the following code the blue rectangle position is set to 120 px + red rectangle position. if I change the red rectangle position , it also changes the blue rectangle position. what do I need to do to not maintain the blue rectangle position(not depending on red rectangle position) ?

    @import Qt 4.7

    Rectangle {
    width: 640
    height: 480

    Rectangle{
        id:redRectangle
        x:10
        y:10
        width:100
        height:100
        color: "red"
    
        MouseArea{
            anchors.fill: parent;
            onClicked:parent.x+=100
        }
    }
    
    Rectangle{
        id:blueRectangle
        x:redRectangle.x+120
        y:10
        width:100
        height:100
        color: "blue"
    
    }
    

    }
    @



  • Are you looking for blueRectangle to initially be positioned at rectRectangle.x + 120, but not bound to it? (so changing redRectangle.x does not cause blueRectangle to move?) In that case you could either use an absolute value for the position of blueRectangle, or assign a value to blueRectangle.x at component completion:

    @
    // this assigns a value, and does not establish a binding
    Component.onCompleted: blueRectangle.x = redRectangle.x+120 @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.