Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qml property vs alias

Tags:

qml

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)

like image 375
amplifier Avatar asked Aug 12 '17 21:08

amplifier


People also ask

What is property alias in QML?

Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).

What is property binding in QML?

Property bindings are a core feature of QML that lets developers specify relationships between different object properties. When a property's dependencies change in value, the property is automatically updated according to the specified relationship.

What is QObject in QML?

The QtObject type is a non-visual element which contains only the objectName property.

What is component onCompleted in QML?

Emitted after component "startup" has completed. This can be used to execute script code at startup, once the full QML environment has been established. The corresponding handler is onCompleted. It can be declared on any object. The order of running the onCompleted handlers is undefined.


2 Answers

There is a very significant difference between your two examples.
A property alias something: id.property automatically creates a bi-directional binding, while in example 1 you only have it one-directional.

Meaning: If you change the value of contentWidth the width of the two Rectangles would change - if now (hypothetically) some inner even in the component Rectangle would change the width, this change would be only reflected in example two, while in example 1 the binding would be broken.

I don't know the implementation details, but it might be easier to optimize it, if you specifically tell, it is not just bound to it, but actually is the same value.

A simple rule of thumbs is: Use property alias ...:... if it is logically an alias, so the properties are intrinsically the same. This is usually the case if you want to export properties from children to the outside, or even whole children.

Use the property binding, if you want to have their value bound because their value coincidencally the same or especially if it is not, but the one is only dependent on the other (but not necessarily other way around, as by default you only have a one-way binding)

A readonly property Item child1: idOfChildOne might have the same effect as property alias child1: idOfChildOne, but semantically the first is like a storage that holds a pointer (that is only forbidden to change, due to the readonly flag) but the second is the direct access to this child. So usually you want to use the second (as done by the QtDesigner).

like image 118
derM Avatar answered Sep 28 '22 18:09

derM


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.

like image 33
seyed sajad Avatar answered Sep 28 '22 18:09

seyed sajad