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
Trusted Contributor
georg22
Posts: 245
Registered: ‎11-22-2011
My Device: Blackberry Playbook
My Carrier: vodafone
Accepted Solution

Remove dynamic created component with QML?

Hi,

 

how can i store the reference of the created object, for example to remove it later.

In the example below the reference "createdControl" is stored in a local variable of the "onClicked" handler and is not accessable later. 

Its not possible to use a property of the Container to store it, because then i need to specify a datatype ( strangely enough its possible to store it in an imported javascript file in a typless variable....). 

 

So this reference seems to be pretty useless for now, or am i missing something?

 

Thanks.

 

Container {              
    id: rootContainer               
    Label  {                  
        text: "Example Component Definition"              
    }              
    Button  {                  
        text: "Click to create dynamic component"
        onClicked :  {                      
            // Create the component and add it to the Container   
            var createdControl = compDef.createObject();
            rootContainer.add(createdControl);
        }             
    }
    attachedObjects: [                  
        ComponentDefinition {                      
            id: compDef                       
            source: "test.qml"             
        }
    ]         
}

 

Please use plain text.
Trusted Contributor
georg22
Posts: 245
Registered: ‎11-22-2011
My Device: Blackberry Playbook
My Carrier: vodafone

Re: Remove dynamic created component with QML?

Ok, the right datatype is "variant", sorry, i searched for an hour, just found it 

Please use plain text.
Regular Visitor
kj13ennett
Posts: 1
Registered: ‎10-19-2012
My Device: BB10
My Carrier: Telus

Re: Remove dynamic created component with QML?

Hey, I'm doing pretty much the same thing, COuld you elaborate on how you solved this, I'm having some trouble still.

 

Thanks

Please use plain text.
Developer
Developer
lew
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha
My Carrier: -

Re: Remove dynamic created component with QML?

something like this should fix it:

 

Container {              

property variant createdControl;

    id: rootContainer               
    Label  {                  
        text: "Example Component Definition"              
    }              
    Button  {                  
        text: "Click to create dynamic component"
        onClicked :  {                      
            // Create the component and add it to the Container   
            var createdControl = compDef.createObject();
if(!createdControl) createdControl = compDef.createObject(); rootContainer.add(createdControl); } } attachedObjects: [ ComponentDefinition { id: compDef source: "test.qml" } ] }

 

Please use plain text.
New Developer
laclekim
Posts: 41
Registered: ‎02-11-2010
My Device: Bold 9000
My Carrier: AT&T

Re: Remove dynamic created component with QML?

OK so how do i call the function to remove. is it destroy or remove...neither of those are working for me.. could you help me with an example?
Please use plain text.
Developer
ekke
Posts: 1,440
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Remove dynamic created component with QML?

I also didn't found a way to destroy dynamically created objects

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
Developer
lew
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha
My Carrier: -

Re: Remove dynamic created component with QML?

to remove:

https://developer.blackberry.com/cascades/reference/bb__cascades__container.html#remove

 

to delete a qml object, use:

destroy()

 

NOTE:

1) when deleting qml objects, it is not garanteed to be freed immediately

2) QML cleans objects automatically within some sort of GC using the parent-child relationship of all the QML objects...so you don't have to clean you dynamically created elements unless you removed it from its parent or its parent won't be deleted until the app is closed

3) to add to pt (2), destroying a parent object also destroys all its childs.

4) destroy are often used in a NavigationPane or TabbedPane....if you have many different Pages or Tabs, you might want to destroy them when it is no longer active, else they won't be cleaned up until the NavigationPane or Tabbedpane is destroyed (for some cases, it could be when the app exits...and that's not good)

5) play around with the above while monitoring the heap usage using the QNX System Information Perspective!

 

hope this helps!

Please use plain text.
Developer
ekke
Posts: 1,440
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: Remove dynamic created component with QML?

thx lew :smileyhappy:

 

now it works

 

I had to use my custom QML Type: (UsersNavPane)

TabbedPane {
    property UsersNavPane n;  

 then to create the dynamic object this way

function createUserTabComponent() {
    n = onDemandComponentUsersNavPane.createObject();
    usersTab.content = n;

 and destroying works this way

function destroyUserTabComponent() {
    if (usersTab.content) {
        usersTab.resetContent();
        n.destroy()
    }

 

thx

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.