Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can read parent Properties from Javascript but not per Bindings - Bug or Feature?



  • [PropRoot.qml]

    import QtQuick 2.0
    
    Item {
        property int propRoot: 123
    
        Timer {
            interval: 500
            running: true
            repeat: true
            onTriggered: propRoot = propRoot+1
        }
    
        PropChild {
        }
    }
    

    [PropChild.qml]

    import QtQuick 2.0
    
    Item {
        //property int propFromRoot: propRoot // ReferenceError: propRoot is not defined (as excepted)
    
        function func(n){
            console.log("propRoot: "+propRoot); // gets access to the current value???
        }
    
        Timer {
            interval: 500
            running: true
            repeat: true
            onTriggered: func()
        }
    }
    

    prints 123,124,125 etc.

    Is that a bug because it breaks the scoping or a needfull feature?


  • Qt Champions 2018

    That's not a bug and it is a documented behaviour ( https://doc.qt.io/archives/qt-5.6/qtqml-documents-scope.html#component-instance-hierarchy ) called dynamic scoping.

    But keep it mind it's considered bad practice and might be removed in Qt 6.

    In your case you should have in PropChild:

    property int propFromRoot: propRoot
    

    and use it like so in PropRoot:

    id: root
    property int propRoot: 123
    /* ... */
    PropChild {
        propFromRoot: root.propRoot
    }
    

Log in to reply