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

qml, qt

Solution

The order is undefined:

Emitted after the object has been instantiated. 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.

https://doc.qt.io/qt-6/qml-qtqml-component.html#completed-signal

Problem

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.

Original source