Unable to create a new object in Qml/javascript
-
Hello,
I connect a function to a signal on an object at component creation like so.
In reaction to the event, I am trying to create an object and place it in a varaible...
However, this does not seem to work as I get and "TypeError: Type error" error at run time on the var m2= new obj2(); obj2 is of course registered with qml.
I tried it with var rr= new Rectangle(); but got the same result...Component.onCompleted: { obj.onSomething.connect(function() { console.log("here"); // This works well... var m2= new obj2();
Any clue what is going wrong?
Thanks
-
Hi
Are you sure "new" should be used and not createObject ?http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html
-
Hello,
I honestly do not know as I am new to Qml...
obj2 is a C++ class and I want to get a new instance of it to use in the rest of the rest of my function.
How would I use createObject in this case?
Cyrille
-
Hello,
I honestly do not know as I am new to Qml...
obj2 is a C++ class and I want to get a new instance of it to use in the rest of the rest of my function.
How would I use createObject in this case?
Cyrille
@Cyrille-de-Brebisson
Hi
Im also a new to QML so i might not be the best to answer.
However, it does sound like this case
http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
and still using createObject to get a new (c++) typein QML. -
Hello,
In the example, the "BackEnd" object is statically created in Qml, not dynamically done...
So, I do not think that it will be useful here :-(.Cyrille
-
Did you get a chance to look "Dynamic QML Object Creation from JavaScript" in qml. Documentation has more details on how to do it. @mrjj is pointing your towards that direction.
-
Hello,
The following seems to work:
var m2 = Qt.createQmlObject('import HPACPinkyQml 1.0; HPACPinkyQml { id: test; linkId:'+linkId+'}', pagesModel, "myfile");But it seems weired that I have to pass text that then needs to be parsed to do this... Especially as it makes passing "arguement" that are more complex than integers and the like quite tricky...
Is there no way to do the same thing without going through the compiler?
Cyrille