qtqmlcomponentscreateobject

Connect to all QML objects created via single Qt.createComponent call


Question

How to create QML component in such a way that all objects created by the component will be connected in same way?

E.g. I would like the triggered signal of all objects created by thirdPartyAPI to be connected to onTriggered slot:

function onTriggered() { }
thirdPartyAPI.factory = Qt.createComponent("Trigger.qml")

Why?

I know I can create component, then create objects by the component and then parametrize (connect and set properties) each of created objects.

I use a 3rd party API that takes a component and creates objects inside. I have control over creation of the component but have no access to Component.createObject calls (it's inside the API).

I would like to obtain an effect similar to this: (I know there's no newObjectCreated signal on component)

var factory = Qt.createComponent(...)
factory.onNewObjectCreated: {
  newInstance.triggered.connect(onTriggered)
}

Solution

  • You can just wrap the object creation procedure into some common function, for example:

    function createMyObject(component, parent, params)
    {
        var compo = Qt.createComponent(component);
        var obj = compo.createObject(parent, params);
        obj.customSignal.connect(onInstanceTriggered);
    }