I want to dynamically create an object C++from QML. I created a derived class QObjectwith the name Carand exposed it QMLwith qmlRegisterType<Car>("org.qtproject.models", 1, 0, "Car");. Inside QMLI can create an instance of an object Caras follows:
Car {
id : car_1
carName : "H1"
carBrand : "Hummer"
carPrice : 125000
}
and then use the object car_1and pass it back on C++with ease if I need to. But I would like to create a dynamic object Carin QML, so I can pass it back to C++.
I tried:
MouseArea
{
anchors.fill: parent
onClicked: {
component = Qt.createQmlObject("Car { id: car_1; carName : \"H1\"; carBrand : \"Hummer\"; carPrice : 125000; }",
parent, "dynamicSnippet1");
myCarModel.appendRowFromQml(component);
}
}
but no luck. When using the static approach, it works fine:
MouseArea
{
anchors.fill: parent
onClicked: {
myCarModel.appendRowFromQml(car_1);
}
}
C++ QML? Qt.createComponent, *.qml, Car, Car C++.