I am trying to dynamically create a element using QT, however it doesn't work and no error message is printed. Compoenent.Status is never ready. It doesn't even go to Compoenet.Error stage :(
Component.onCompleted: {
var Component = Qt.createComponent("parts/Column.qml");
console.log(Component.errorString());
The Column.qml basically contains a ColumnLayout element. What am I doing wrong ?
Creating components dynamically in QML is a multistep process. Calling Qt.createComponent() is only the first step. The documentation on this process goes into some detail.
In many simple cases, the component loads immediately and you can just do this:
// Create our Component
var myComponent = Qt.createComponent("foobar.qml");
// Instantiate the Component's object, give it a parent, and set its properties
var foobar = myComponent.createObject(parentObject, {"x": 0, "y": 0});
But in more complex cases, you have to attach to the component's statusChanged signal. A complete example of this is in the documentation above, I've copied and pasted it here for reference:
var component;
var sprite;
function createSpriteObjects() {
component = Qt.createComponent("Sprite.qml");
if (component.status == Component.Ready)
finishCreation();
else
component.statusChanged.connect(finishCreation);
}
function finishCreation() {
if (component.status == Component.Ready) {
sprite = component.createObject(appWindow, {"x": 100, "y": 100});
if (sprite == null) {
// Error Handling
console.log("Error creating object");
}
} else if (component.status == Component.Error) {
// Error Handling
console.log("Error loading component:", component.errorString());
}
}