dynamic_cast<type> equivalent in Qml?
-
Hello,
I have a C++ class (object factory), which notifies Qml when various object types are created with an onNewObject(object) signal.
Qml creates the appropriate control (depends on a "type" property) and "assign" the object to it.
Something like the pseudo code below.
The problem that I am having is that Qml refuses to assign my "object" to the property as object is a QObject and it does not cast it to the proper type (or try to).
So, my question is: how do I do the equivalent of a C++ dynamic_cast in Qml?
Thanks.Component {
id: componentType0
property MyType0 source
Rectangle { width: 10; height: 10; color: "Red" }
}Component {
id: componentType1
property MyType1 source
Rectangle { width: 10; height: 10; color: "Blue" }
}onNewObject: { console.log("new object type:", object.type)
switch (object.type) {
case 0:
componentType0.createObject(appWindow, {"source": object});
break;
case HPACDevices.Brain:
componentType1.createObject(appWindow, {"source": object});
break;
default: return;
}
} -
You cannot have property like property MyType0 source inside the component directly. I'm sure this is just a pseudo code.
Instead of trying the MyType you can try with property var. Just see the following example.
Component { id: componentType0 Rectangle { property var source; width: 100; height: 100; color: "Red" onSourceChanged: { source.visible=true; } } }
Everything should work.