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
jamiejulius
Posts: 49
Registered: ‎06-06-2012
My Device: Bold 9700
Accepted Solution

QML: Accessing variables defined outside a list component from within a list component

Consider the following QML:

 

import bb.cascades 1.0


Page {
  content: Container {
    Label {
      id: topLabel
      text: "One"
      textStyle.size: 100.0
      textStyle.color: Color.Blue
    }

    Button {
      text: "Change to Two"
      onClicked: {
        topLabel.text = "Two"
      }
    }


    ListView {
      objectName: "testList"

      listItemComponents: [
        ListItemComponent {
          type: ""
          Button {
            text: "Change to Three"
            onClicked: {
              topLabel.text = "Three"
            }
          }
        }
      ]

      maxHeight: 300.0
    }
  }
}

 

The pages defines three controls: a label, a button and a listview. In turn, each listview item also contains a button.

 

Clicking on the button beneath the label successfully changes the label's text via this code:

 

onClicked: {
  topLabel.text = "Two"
}

 

However, executing similar code from the button within the listview fails with this error:

 

Test.qml:28: ReferenceError: Can't find variable: topLabel

 

Apparently, variables defined outside of the listview are not directly visible from within the listview items.

 

Is there some syntax available in order to access them?

 

Thanks,

 

Jamie

 

 

 

 

BlackBerry Development Advisor (Retired)
gperry
Posts: 138
Registered: ‎05-11-2012
My Device: Developer

Re: QML: Accessing variables defined outside a list component from within a list component

Are you looking for a way to set the button text based on the selection change in the list?

 

The reccommended way is shown at the bottlom of the page below.

 

https://developer.blackberry.com/cascades/documentation/ui/lists/list_view.html

 

 

Alternatively you might need to use a property to expose the label text.

Developer
jamiejulius
Posts: 49
Registered: ‎06-06-2012
My Device: Bold 9700

Re: QML: Accessing variables defined outside a list component from within a list component

Hi,

 

The text I'm trying to change is that of the top label. It already has a property (text) which exposes that. I'm trying to do it in response to a button, which is defined within a ListComponent. The problem seems to be that the label (which is outside of the list) is not programmatically accessible from within the ListComponent (which is inside of the list). The variable (the id of the label) can't be found.

BlackBerry Development Advisor (Retired)
gperry
Posts: 138
Registered: ‎05-11-2012
My Device: Developer

Re: QML: Accessing variables defined outside a list component from within a list component

OK, We have identified a work around that should solve your issue, as suspected it was scope related so some redefinition is required.

 

At the top Page level add the following line to make the label accessible.

 

Page {
    id: topPage
    
    onCreationCompleted: { Qt.topLabel = topLabel; }
        
Then in the button definition you can reference the Qt.topLabel from within the list.

 

            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    Button {
                        text: "Change To Three"
                        onClicked: {
                            Qt.topLabel.text = "Three";
                        }                        
                    }
                }
            ]

 

Hope this resolves your issue. It certainly worked on my system.

 

Graham

Developer
jamiejulius
Posts: 49
Registered: ‎06-06-2012
My Device: Bold 9700

Re: QML: Accessing variables defined outside a list component from within a list component

Works for me too. Thanks for your time on this!

 

I since learned that the listitem button was not a child in the same tree, so I understand what you mean about a different scope. Nonetheless, I was stumped to find a simple solution.

 

To understand your solution, I gather that Qt is some kind of pre-existing global object. Correct? And what you are essentially doing, is adding a dynamic property (i.e., one that didn't already exist). Correct?

 

Thanks,

 

Jamie

 

 

BlackBerry Development Advisor (Retired)
gperry
Posts: 138
Registered: ‎05-11-2012
My Device: Developer

Re: QML: Accessing variables defined outside a list component from within a list component

Correct.

 

Can you mark this thread as solved please.

 

Glad we were able to help.

 

Graham

Developer
jamiejulius
Posts: 49
Registered: ‎06-06-2012
My Device: Bold 9700

Re: QML: Accessing variables defined outside a list component from within a list component

Thanks!

 

I marked this as solved before my previous reply.

 

Cheers,

 

Jamie

BlackBerry Development Advisor (Retired)
gperry
Posts: 138
Registered: ‎05-11-2012
My Device: Developer

Re: QML: Accessing variables defined outside a list component from within a list component

Ooops,

 

Thanks

Graham

Developer
singpolyma
Posts: 48
Registered: ‎11-15-2012
My Device: Q10

Re: QML: Accessing variables defined outside a list component from within a list component

Would there be some way to access this by going up through the parent accessor or similar, without resorting to a global namespace hack?

Developer
Zmey
Posts: 1,514
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QML: Accessing variables defined outside a list component from within a list component

[ Edited ]

Yes, ListView's properties and methods (which reside in global context) can be accessed from ListItemComponent as topLevelItemId.ListItem.view.funcOrPropName

 

Sample code:

http://supportforums.blackberry.com/t5/Cascades-Development/ActionItem-issue-in-a-context-menu/m-p/2...


Andrey Fidrya, @zmeyc on twitter