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
testinz
Posts: 183
Registered: ‎09-03-2012
My Device: Blackberry 10
Accepted Solution

Cannot access context property from inside ListView checkbox - onCheckedChange event

I have a context property call "app" that links to my C++ application. My listview has a checkbox with onCheckedChange event listener. In that method, when I make a app.runAction call to the application C++ side., the system displays cannot find app error.

 

If I move the Checkbox outside of the list view, it will trigger app.runAction correctly.

 

Could you please take a look? Thanks!

 

C++ side on how I defined the context property.

 

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    if (qml->hasErrors()) {
        return false;
    }

    qml->setContextProperty("app", this);

    navigationPane = qml->createRootObject<NavigationPane>();
    if (!navigationPane) {
        return false;
    }

 

QML Side

 

NavigationPane {
    id: nav

Page {
        id: mainPage
        Container {

ListView {    

listItemComponents: [
        ListItemComponent {
                        type: "item"
                        Container {

                   CheckBox {
                                // Determine whether the CheckBox should be checked
                                // according to a value in the data model
                                checked: ListItemData.checked
                                onCheckedChanged: {
                                     app.testAction();
                                }
                            }

Developer
Developer
lew
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha

Re: Cannot access context property from inside ListView checkbox - onCheckedChange event

it is a scoping issue...i really hope that the devs find a way to fix that properly aside from this ugly solution:

 

you can solve this by setting your app variable into the global object Qt

 

NavigationPane {
    id: nav

onCreationCompleted: Qt.app = app

Page {
        id: mainPage
        Container {
ListView {    
listItemComponents: [
        ListItemComponent {
                        type: "item"
                        Container {
                   CheckBox {
                                // Determine whether the CheckBox should be checked
                                // according to a value in the data model
                                checked: ListItemData.checked
                                onCheckedChanged: {
                                     app.testAction();
                                     Qt.app.testAction();
                                }
                            }

 

any code within a ListItemComponent will suffer from this 'scoping' issue :smileysad:

Developer
testinz
Posts: 183
Registered: ‎09-03-2012
My Device: Blackberry 10

Re: Cannot access context property from inside ListView checkbox - onCheckedChange event

That works! Thanks lew!
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: Cannot access context property from inside ListView checkbox - onCheckedChange event

I would argue that you probablly shouldnt use the Qt global scope, but who knows.

 

You can always access the list view from your listitemcomponent and could put a function in there that acts a proxy to your context property

 

componentId.ListItem.view.myFunction()

Like all of my posts