Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 59
Registered: ‎09-28-2012
My Device: BlackBerry 10
Accepted Solution

Dynamically create QmlComponentDefinition objects in code

With the sample QML:

// --- main.qml ---
Container {
    objectName: "fooContainer"
    attachedObjects: [
        ComponentDefinition {
            id: barComponent
            source: "Bar.qml"
        }
    ]
}

// --- Bar.qml ---
Container {
objectName: "bar"
    Label {
text: "FooBar"
    }
}

 I'm trying to dynamically create barComponent objects and add them to fooContainer within the C++ code.

So I reference the fooContainer using:
    

fooContainer = createdRootObject->findChild<Container*>("fooContainer"); 

But how do I reference the barComponent QML definition to use it's createObject() method?

Highlighted
Developer
Posts: 410
Registered: ‎06-03-2010
My Device: Z10 Red
My Carrier: Free

Re: Dynamically create QmlComponentDefinition objects in code

[ Edited ]

The component definition is used to create object from QML

 

To create object from C++, you have to do:

 

qml = QmlDocument::create("asset:///Bar.qml");
Control *root =  qml->createRootObject<Control>();
fooContainer->add(root)

 

Developer
Posts: 59
Registered: ‎09-28-2012
My Device: BlackBerry 10

Re: Dynamically create QmlComponentDefinition objects in code

That worked, thanks.