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
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo
Accepted Solution

Manipulating dynamically created qml components in C++

Hey,

I've dynamically create a qml page using the following code:

                onClicked: {
                    var favPage = favPageDef.createObject();
                    nav.push(favPage);    
                }
                attachedObjects: ComponentDefinition {
                    id: favPageDef
                    source: "FavPage.qml"                        
                }

 I would now like to manipulate components in this newly created qml page in c++. How do access these components?

 

I have been able to access qml components quite easily if they were created when the app initialized but obviously this is not the case.

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Manipulating dynamically created qml components in C++

You can use a property on the containing object (or another) to keep a reference to it outside of the JavaScript routine, whose scope is limited to the onClicked block.

Button {
    id: myButton

    property variant myPage

    onClicked: {
        var favPage = favPageDef.createObject();
        nav.push(favPage);    
        myPage = favPage; // save ref for use elsewhere
    }
    attachedObjects: ComponentDefinition {
        id: favPageDef
        source: "FavPage.qml"                        
    }
}

...
// elsewhere:
Button {
    text: "Show result"
    onClicked: {
        console.log("prop is", myButton.myPage.someProperty);
    }
}

 

Note that if your behaviour upon closing the page is to do page.destroy() (in the NavigationPane) you might run into trouble as the reference may no longer be valid.  In such a case, you might want to extract the required data in the onPopTransitionEnded handler, or something similar.  (Repeatedly creating new pages and never destroying them would be a bad idea too... memory leak!)

 


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: Manipulating dynamically created qml components in C++

if you don't want to destroy the page and reuse

you could do it this way

 

...

property Page myPage

...

if (! myPage) {

    myPage = ....definition.createObject();

}

 

as Peter said: it's really important not to repeatedly create Objects without destroying them or reuse the one created first time

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

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Highlighted
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Manipulating dynamically created qml components in C++

I found out what the problem was. I needed to make my AbsractPane public in my header file. Then when I created the qml component and called a c++ function it was found to be a child. Thanks for everyone's help though.