0

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 ?

2
  • Isn't Component a reserved keyword? Have you tried naming that variable something else? Commented Jul 21, 2014 at 19:13
  • @MrEricSir I realized that after writing the question, tried a different variable, still doesn't work. Commented Jul 22, 2014 at 1:19

1 Answer 1

2

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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.