0

when applying property bindings in QML I have encountered one problem. When we have a parent component (Window) and a child (Rectangle), which has some properties bind to parent's (width, height, or anchors.fill: parent), when I change parents properties in JS code, and if I want to read the values of the child's properties (that are bound to parent's) in the same JS code, it shows the old values (not updated). It looks like that the change of parents properties hasn't been propagated to child's. Here is the example of this problem:

Window {
id:myWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
    id:customRec
    width:parent.width
    height: parent.height
    color: "blue"
}
Button{
    id:myBtn
    onClicked: {
        myWindow.width = 800
        myWindow.height = 600
        console.log(customRec.width)
        console.log(customRec.height)
    }
}}

After clicking on the button, it shows: qml: 640 qml: 480 instead of 800 and 600, new values. Although the rectangle has been scaled well. After clicking again it will show updated values (800 and 600). Can someone please explain what is happening here and how can binding property change be propagated immediately to bound properties. I am using Qt 5.12.2 with msvc2017_64 compiler.

1 Answer 1

1

You are printing the properties before they got updated. With the below code you can find that onWidthChanged signal comes after the console log. onWidthChanged signal comes after updating the width.

import QtQuick 2.10
import QtQuick.Window 2.10
import QtQuick.Controls 2.2

Window {
    id:myWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle{
        id:customRec
        width: parent.width
        height: parent.height
        color: "blue"
        onWidthChanged: console.log("***********")
    }
       Button{
        id:myBtn
        width: 100
        height: 100

        onClicked: {
            myWindow.width = myWindow.width +50
            myWindow.height = myWindow.height +50
            console.log("--------------------------")
            console.log("window width" + myWindow.width)
            console.log("window height" + myWindow.height)
            console.log("customrect width" + customRec.width)
            console.log("customrect height" + customRec.height)
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.