Qml property vs alias

Viewed 16292

To get values of properties of parent component and assign them to child properties we can use parent properties directly

//Component1.qml:

Item
{
    Component2
    {
        contentWidth:200
    }
}

//Component2.qml:

Item
{
    property int contentWidth:0
    Rectangle
    {
        width:parent.contentWidth
    }
}

or create an alias

//Component1.qml:

Item
{
    Component2
    {
        contentWidth:200
    }
}

//Component2.qml:

Item
{
    property alias contentWidth:rect.width
    Rectangle
    {
        id:rect
    }
}

What is the most appropriate way and when?

My thought is that alias should be used when parent property is intended only for one particular child component property (contentWidth is intended only for rect.width)

2 Answers

Using alias means use another name for property like C++ reference type declaration it must been init at time that has been created. The main usage of alias is to ability to export inner scope property to outer scope of object scope. Also we can use property to achieve this goal across binding feature of qml. There is significant different between alias and simple property for exporting. If we use property to send to outer scope variable is not problem if we want to use it just as for reading. The problem come when we want to bind that property to new value to reassignement to another static value; in this time we lose the property binding to our internal property. So if we to export just use alias.

Related