Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of Component.onCompleted in a QML file with many items?

Tags:

qt

qml

For example, consider the code below:

Rectangle {
    id: idRectParent

    Rectangle {
        id: idRectChild1
        component.onCompleted: {
            console.log("Iam Child 1")
        }
    }

    Rectangle {
        id: idRectChild2
        component.onCompleted: {
            console.log("Iam Child 2")
        }
    }

    component.onCompleted : {
        console.log("Iam parent Rect")
    }
}

I'm getting the output below if I run it in qmlscene (I have tried almost 50 times).

Iam parent Rect
Iam Child 2
Iam Child 1

Why is the output in the above order, instead of:

Iam parent Rect
Iam Child 1
Iam Child 2

or

Iam Child 1
Iam Child 2
Iam parent Rect

or any other combination.

like image 517
Mahesh Avatar asked Jul 04 '14 15:07

Mahesh


People also ask

How do you destroy a component in QML?

Check box on, create component. Check box off, destroy component.

What is loader in QML?

Loader is used to dynamically load QML components. Loader can load a QML file (using the source property) or a Component object (using the sourceComponent property).

What is property alias in QML?

Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).

How do you create a component in QML?

Creating a Component Dynamically To dynamically load a component defined in a QML file, call the Qt. createComponent() function in the Qt object. This function takes the URL of the QML file as its only argument and creates a Component object from this URL.


1 Answers

The order is undefined:

Emitted after component "startup" has completed. This can be used to execute script code at startup, once the full QML environment has been established.

The corresponding handler is onCompleted. It can be declared on any object. The order of running the onCompleted handlers is undefined.

http://qt-project.org/doc/qt-5/qml-qtqml-component.html#completed-signal

like image 54
Mitch Avatar answered Oct 16 '22 18:10

Mitch