Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add property dynamically to a QML element?

Tags:

qml

I want to add properties dynamically to a QML element:

Item {
 id: dynamicProperty;
 property int first;


 Component.onCompleted: {
  /* once this block of code is executed, i want to add
     property int second; property bool third; property variant fourth;
  */
 }

}

Is there any way to accomplish the above task.

like image 566
Kalpesh Jain Avatar asked Sep 24 '12 11:09

Kalpesh Jain


People also ask

How to create object dynamically in QML?

Creating a Component Dynamically To dynamically load a component defined in a QML file, call the Qt. createComponent() function in the Qt object. This function takes the URL of the QML file as its only argument and creates a Component object from this URL.

How do you use property in QML?

A property is an attribute of an object that can be assigned a static value or bound to a dynamic expression. A property's value can be read by other objects. Generally it can also be modified by another object, unless a particular QML type has explicitly disallowed this for a specific property.

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

On the one hand: I don't see, why anyone would want to do this, as it is completely non-declarative. However, as QML extends JavaScript, and latter is a prototyping-language, yes, you can do it.

On the how-to, I reccomend to read the JS-Documentation on how to define properties. However I wrote a short example, to demonstrate the use of it.

MouseArea {
    anchors.fill: parent
    onClicked: console.log(rect.newProp)
}

Rectangle {
    id: rect
    width: 50
    height: 50
    x: 50
    y: 50
    color: 'green'

    MouseArea {
        anchors.fill: parent
        onClicked: { var obj = Object.defineProperty(rect, 'newProp',
                                                     {
                                                         enumerable: false,
                                                         configurable: false,
                                                         writable: false,
                                                         value: '50'
                                                     })}
    }
}

On the first click on the background, 'undefined' will be printed. After clicking on the rectangle, this will change to '50'.

like image 115
derM Avatar answered Sep 18 '22 07:09

derM


I don't think QML was designed to support dynamic property creation.

Depending on your use-case this could be worked around with "script instances" (I made up the term). Basically each QML Component instantiation that import script which doesn't have .pragma library statement will work with its own copy of globals declared in the script.

For example you can look at PageStack (qt-components) code.

There is import "PageStack.js" as Engine statement, and it later referred to call functions that uses global variables like if it was instance variables.

If you looking for a way to add members to your QML Component and don't need property change notifications, "script instances" would do just fine.

like image 34
sergk Avatar answered Sep 17 '22 07:09

sergk