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
strobejb
Posts: 267
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange
Accepted Solution

QML createObject with parameters

Is it possible to pass parameters to the QML createObject function?

 

In QML we use createObject to dynamically create objects from a component definition.

 

i.e.

attachedObjects: [

  ComponentDefinition {

     id: myDef

     Container {

         property int fooBar

     }

  }

]

 

var myobject = myDef.createObject()

 

Now, according to the documentation below we can pass a JSON structure to initialize the new object's properties, which get setup before object creation and allow things like bindings to work correctly:

 

https://developer.blackberry.com/cascades/reference/qml-component.html#createObject-method

 

In theory I should be able to do this:

 

var myobject = myDef.createObject(null, {"fooBar": 1234} )

 

I cannot get this to work however - does anyone have a workaround?

 

Please use plain text.
Developer
simon_hain
Posts: 15,956
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: QML createObject with parameters

the code looks good to me, did you try to pass a parent? i usually just use "this", maybe the null is not to its taste.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
gyubok
Posts: 519
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: QML createObject with parameters

Yea, that code wouldn't work.

 

Try this

 

var component = Qt.createComponent("MyPage.qml");
var mypage = component.createObject(null, {"fooBar": 1234});
mycontainer.add(mypage)

 That should work.

Please use plain text.
Developer
strobejb
Posts: 267
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: QML createObject with parameters

Hi, thanks for your reply. Yes, I tried passing a parent but the result was the same. This seems to be an issue because the cascades ComponentDefinition createObject function only takes the 'parent' parameter and nothing else.
Please use plain text.
Developer
strobejb
Posts: 267
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: QML createObject with parameters

thanks, this is a nice suggestion - i'll give it a try
Please use plain text.
New Developer
mobilesync
Posts: 11
Registered: ‎09-29-2012
My Device: 9900
My Carrier: *

Re: QML createObject with parameters

This works. Please flat as solution.
Please use plain text.