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
d3v3l0p3r
Posts: 168
Registered: ‎07-19-2012
My Device: Q5, Q10, Z10, Z30, PlayBook
Accepted Solution

ListView cannot see C++ object passed to QML via QmlDocument->setContextProperty()

I can access an object passed from C++ using the QmlDocument->setContextProperty() function in JavaScript inside most QML elements (such as Container, ImageButton, etc) but cannot access it from the JavaScript inside ListView. I get "ReferenceError: Can't find variable: app". Why is that?

---
If you find my post helpful please "like" it and "accept as a solution"
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: ListView cannot see C++ object passed to QML via QmlDocument->setContextProperty()

Maybe you can shrink your code down to a postable subset which reproduces the problem, so others can try it out for themselves. Usually at that point someone will see what the problem is... most often the one doing the shrinking. ;-)


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!
New Developer
fetcher
Posts: 9
Registered: ‎01-11-2013
My Device: BB10

Re: ListView cannot see C++ object passed to QML via QmlDocument->setContextProperty()

I have exactly the same problem

The qml code is below:

        ListView {
        dataModel: XmlDataModel {
            source: "models/items.xml"
        }
        // Add appearance definitions for the list items using the
        // listItemComponents list
        listItemComponents: [
		ListItemComponent {
			type: "header"
			Label {
			   text: ListItemData.title
			   textStyle {
				base: SystemDefaults.TextStyles.NormalText
				fontWeight: FontWeight.Bold
				color: Color.create ("#7a184a")
		           }
			}
		},
		ListItemComponent {
			type: "listItem"
			Container {
			    layout: StackLayout {
			    orientation: LayoutOrientation.LeftToRight
			    }
			CheckBox {
				checked: _basic.getValueFor("myobject", "true")
                onCheckedChanged: {
                    _basic.setValueFor("myobject", checked);
                                        
                }
            }
			Label {
				text: ListItemData.title
				textStyle {
				    base: SystemDefaults.TextStyles.NormalText
				    fontWeight: FontWeight.Normal
				}
				}
				
		} // end of Container
} // end of second ListItemComponent
] // end of listItemComponents list
} // end of ListView        
        

 The idea is to populate a list with headers and checkboxes and labels. The datamodel comes from an xml file.

The _basic is defined as     qml->setContextProperty("_basic", this);


When I set a breakpoint on setValueFor or getValueFor, those functions are never called.

 

From the qml file above, if I copy the Checkbox {} statement out of the listView statement, the functions are called correctly.

 

New Developer
fetcher
Posts: 9
Registered: ‎01-11-2013
My Device: BB10

Re: ListView cannot see C++ object passed to QML via QmlDocument->setContextProperty()

Seems that this is a known issue (http://supportforums.blackberry.com/t5/Cascades-Development/ContextActions-inside-ListItem-can-not-a...)

 

This is still not fixed in 10.0.9.2318.

Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: ListView cannot see C++ object passed to QML via QmlDocument->setContextProperty()

Why is it an "issue"? Thats probably just the way things work and ought to work. ListItemComponents are not really part of the scene graph/context since they just exist to be used at runtime. So I would expect the scope of the listItemcomponent to be different than the rest of the document.

 

The way to get around that, as described in the thread linked above is to create a function inside your listview that references the context property that you want to use. Then your listitemcomponent has access to that listview function.

Like all of my posts
New Developer
fetcher
Posts: 9
Registered: ‎01-11-2013
My Device: BB10

Re: ListView cannot see C++ object passed to QML via QmlDocument->setContextProperty()

Yes it makes sense.

 

However, from my point of view (and related to the few posts/questions related to this topic) I would expect a consistent API whatever the lower level dependencies are.

 

Thanks