Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Can read parent Properties from Javascript but not per Bindings - Bug or Feature?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 154 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LowLevelM
    wrote on 20 Sept 2019, 10:13 last edited by LowLevelM
    #1

    [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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GrecKo
      Qt Champions 2018
      wrote on 20 Sept 2019, 12:19 last edited by
      #2

      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
      }
      
      1 Reply Last reply
      3

      1/2

      20 Sept 2019, 10:13

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved