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
New Contributor
Posts: 6
Registered: ‎10-14-2010
My Device: BB10 - Developer
My Carrier: Movistar
Accepted Solution

Nested QML Data Management

[ Edited ]

Hi, everyone, 

 

I'd like to know how to use the context variables in QML in nested files, because they are not working for me. 

 

I declared in mainApp.cpp a variable, as usual: 

qmlHome->setContextProperty("mainApp", this);

 

And I have two nested QML like this:

 

QML1.qml

 

Page{

 Container{

        //...some content...

        //...somewhere call mainApp.someFunction() and it's working well

 

 

        QML2{

               id: qml2

}

}

}

 

QML2.qml

 

Container{

     //...some content here...

     //...somewhere I call mainApp.someFunction() or any other function and the error says: mainApp object not found

}

 

Is there any way to know the scope of the functions created in our .cpp files when set to be read in QMLs with setContextProperties ?

 

This happens when I have two or more levels of nested files.

 

Thanks in advance for your help. 

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Nested QML Data Management

It should work unless QML2 is inside of ListItemComponent which has it's own context.

Andrey Fidrya, @zmeyc on twitter
New Contributor
Posts: 6
Registered: ‎10-14-2010
My Device: BB10 - Developer
My Carrier: Movistar

Re: Nested QML Data Management

Yes, the second QML is inside a ListView with two ListItemComponents. 

 

Can I give them the content of the parent context?

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Nested QML Data Management

[ Edited ]

Yes, but it's a bit complicated.

 

Declare a JavaScript function inside of ListView { }. From this function you can access variables exported from C++ code.

 

From inside of ListItemComponent items this function can be called like this:

 

rootComponent.ListItem.view.myFunction()

Where rootComponent is id of top level item declared inside of ListItemComponent.

 

This page has more details on how ListView works and it's context:

http://developer.blackberry.com/cascades/reference/bb__cascades__listview.html

 

Some sample code:

http://supportforums.blackberry.com/t5/Cascades-Development/Access-ListView-model-from-ListItemCompo...

In this code ListView's dataModel property is being accessed, but calling the function is similar.

 

Function declaration looks like this:

ListView {
  function myFunction()
  {
    return mainApp.myVariable // or function()
  }
  ...

 


Andrey Fidrya, @zmeyc on twitter
New Contributor
Posts: 6
Registered: ‎10-14-2010
My Device: BB10 - Developer
My Carrier: Movistar

Re: Nested QML Data Management

Thanks a lot, Zmey. 

 

What you told me guided me to the right answer. 

 

I did as follows: 

 

I created a function in the ListView's root.

function accessExternalFunction(){
      return mainApp.myFunction();
}

 

then, inside the ListItemComponent I called that function like this:

parent.accessExternalFunction();  

 

I don't know why this worked, but it did.