Michał Walenciak
Michał Walenciak

Reputation: 4369

QML: Injecting Item into hierarchy of Items

I have a reusable item which looks like this:

Reusable.qml:

Flickable {
    CustomCppComponent {
        id: customComponent
    }
}

Now I'd like to instantiate it with some extra child:

Main.qml:

Item {
    Reusable {
        Rectangle {
            id: rect
            anchors.fill: parent
        }
    }
}

But I want Rectangle to be placed as a child of CustomComponent so the equivalent would be:

Main.qml:

Item {
    Flickable {
        CustomCppComponent {
            id: customComponent
            Rectangle {
                id: rect
                anchors.fill: parent
            }
        }
    }
}

How do I do that?

Upvotes: 0

Views: 166

Answers (1)

JarMan
JarMan

Reputation: 8277

The easiest way is to use a default property.

Reusable.qml:

Flickable {
    default property alias innerContents: customComponent.data
    CustomCppComponent {
        id: customComponent
    }
}

Since innerContents is the default property, any child objects that you add to your Reusable instance will get added to innerContents, which is pointing at customComponent's data property.

This assumes that CustomCppComponent is a visual item of course, which your question did not define.

Upvotes: 3

Related Questions