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. Binding expression for nested properties not working
Forum Updated to NodeBB v4.3 + New Features

Binding expression for nested properties not working

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlbindproperty bindinbinding
2 Posts 2 Posters 1.2k Views 2 Watching
  • 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.
  • B Offline
    B Offline
    BePie
    wrote on 18 Jan 2018, 08:06 last edited by BePie
    #1

    If there is an object like this:

    //A.qml
    import QtQuick 2.0
    import QtQml 2.0
    
    Rectangle {
    
        property QtObject props: QtObject {
            property color mainColor: "blue"
        }
    
        color: props.mainColor
    
    }
    

    And I use this object, properties of A instance's props properties (like prop.mainColor) can be read, imperative assigned to, assign to a binding. The problem is that binding expression does not work:

    import QtQuick 2.9
    import QtQml 2.0
    import QtQuick.Window 2.2
    
    Window {
        A {
            id: a
            anchors.fill: parent
    
            props.mainColor: "red" // <<< error
    
            Component.onCompleted: {
                a.props.mainColor = Qt.binding(function(){return "red"})  // <<< wo
                a.props.mainColor = "red"  // <<< works
                console.log(a.props.mainColor) // <<< works
            }
        }
        // Component.onCompleted works also in this scope
    }
    
    

    error (if property QtObject props: QtObject{...}):
    Cannot assign to non-existent property "mainColor"

    error (if property var props: Item{...}):
    Invalid grouped property access

    Can someone explain why one can read the property (and assign) but not bind-assign?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Timothy 0
      wrote on 3 May 2023, 21:04 last edited by
      #2

      I just ran into this issue myself, and I have a solution. I know this is an old topic, but since this was pretty much the only thing I could find on the internet about this problem, hopefully this can help someone else.

      In order to bind to a property of the nested QtObject props from the of component A, props needs to be a type of component that is known within the scope where it's properties are being accessed.

      To fix the code above, create a new file for the props Component:

      //Props.qml
      import QtQml 2.0
      
      QtObject {
          property color mainColor
      }
      

      Now update the A component to use the Props component:

      //A.qml
      import QtQuick 2.0
      import QtQml 2.0
      
      Rectangle {
      
          property Props props: Props {
              mainColor: "blue"
          }
      
          color: props.mainColor
      
      }
      

      A few things to note here from my testing:
      If you were to now add a new property to the instance of Props in A.qml above, and try to bind a value to that , it would fail, because in the outside scope, that new property is not a known property of the Props type.
      Similarly, if you were to declare A.props as property QtObject props: Props { ... } then binding to mainColor would fail because mainColor is not a property of QtObject, and props is being exposed as a QtObject (the base class).

      It appears that the property resolution for binding to a nested property is not able to infer additional properties added to an instance of an object, and can only be bound to known properties of the type being exposed.

      1 Reply Last reply
      2

      • Login

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