Search code examples
qmlqt-quick

How to forward properties of a child component?


When writing a wrapper around some components, for example :

MyComponent.qml

ColumnLayout{
    ListView{
        id: listview
    }
}

Is it possible to expose all the properties from a child (listview here) without typing by hand all the aliases ?

So that I can do something similar to what the following would do if it was allowed :

ColumnLayout{
    MyComponent{
        listview.model: ListModel{  // Here
        }
    }
}

(Either by forwarding them, either by defining a property that point to listview (I tried, it does not seems to work...), or any other way that permit to bind a property from listview as I would do if I was defining a ListView)


Solution

  • You just need to alias the id of the object you want to expose. So in MyComponent.qml, you can do this:

    ColumnLayout{
        property alias listview: listview
        ListView{
            id: listview
        }
    }
    

    Then you can access it from other objects:

    ColumnLayout{
        MyComponent{
            listview.model: ListModel{
            }
        }
    }