I have a QML JavaScript function that generates and returns a component called Item
:
function createComponent() {
var component = Qt.createComponent('MyComponent.qml');
var obj = component.createObject(container, {'x': 0, 'y': 0});
return obj; // Unsure whether to return obj or component for C++ usage
}
In my main.qml
, I utilize a custom C++ class:
// ...
import com.acidic.customclass 1.0
import "CreateComponent.js" as CreateComponent
ApplicationWindow {
visible: true
width: 1280
height: 800
CustomClass {
id: customClass
}
Button {
onClicked: {
customClass.receiveComponent(CreateComponent.createComponent)
}
}
}
The header file for my C++ class is as follows:
Q_INVOKABLE void receiveComponent(const QObject& obj /* QObject ref doesn't work */);
And here's the body of the C++ class:
void CustomClass::receiveComponent(const QObject& obj) {
qDebug(obj.property("width")); // Checking if we received it correctly
}
Is there a way to pass a component generated using JavaScript and Qt.createComponent
into the function parameter of my custom C++ class?