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.
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 librarystatement 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 Enginestatement, 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.