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
contryboy
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA
Accepted Solution

ContextActions inside ListItem can not access ContextProperty in QMLDocument

Hi There,

 

What I need to achieve is to operate on a list of data, e.g. delete an item using ContextAction in ListItem in QML, then call a C++ method of an object in QmlDocument's contextProperty. here is how I did it:

C++ code:

QmlDocument *qml = QmlDocument::create("accounts.qml");
root = qml->createRootNode<AbstractPane>();

//Here set the context property, accountsManager was already created
qml->setContextProperty("accountsManager", accountsManager);

//Set the model for the list view
ListView* accountsListView = root->findChild<ListView*>("accountsListView");
accountsListView->setDataModel(accountsManager->getDataModel());

//...

 

QML code:

Page {
    content: Container {        
         
        ListView {
            id: listView
            objectName: "accountsListView"             
            listItemComponents: [
                ListItemComponent {
                    type: "listItem"
                    StandardListItem{
                        id: accountItem
                        title: ListItemData.username
                        
                        contextActions: [
                            ActionSet {
                                deleteAction: DeleteActionItem {
                                    title: "Delete"
                                    onTriggered: {
                                        //it does not work here, "ReferenceError: Can't find variable: accountsManager"
                                        accountsManager.doSometing();
                                    }
                                }
                            }
                        ]
                    }
                } 
            ]
            
            function itemType(data, indexPath) {
                return "listItem";
            }     
        } // end of ListView
    } // end of Container
    
    actions: [
        ActionItem {
            title: "Add account"
            ActionBar.placement: ActionBarPlacement.OnBar
            
            onTriggered: {
                //It works fine here
                accountsManager.doSomething();
            }
        }
    ]
} // end of Page

 

Please use plain text.
Developer
jan_macura
Posts: 20
Registered: ‎08-02-2012
My Device: -
My Carrier: SW Developer

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Hi,

 

same problem, but I have added function to ListView that is calling C++ func. ListView function can be called from ListItem by ListItem.view.func_name()

 

Jan

Please use plain text.
Developer
contryboy
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Hi Jan,

 

Thanks for your reply, I gave it a try, how ever, it still does not work:

Page {
    content: Container {        
         
        ListView {
            id: listView
            objectName: "accountsListView"             
            listItemComponents: [
                ListItemComponent {
                    type: "listItem"
                    StandardListItem{
                        id: accountItem
                        title: ListItemData.username
                        
                        contextActions: [
                            ActionSet {
                                deleteAction: DeleteActionItem {
                                    title: "Delete"
                                    onTriggered: {
                                        //Error: Result of expression 'ListItem.view' [null] is not an object
                                        ListItem.view.doSomething();
                                        //if change to following, another error:
                                        //Result of expression 'accountItem.ListItem.view.doSomething' [undefined] is not a function.
                                        //accountItem.ListItem.view.doSomething();
                                    }
                                }
                            }
                        ]
                    }
                } 
            ]
            
            function itemType(data, indexPath) {
                return "listItem";
            }     
            function doSometing() {
	        accountsManager.doSomething();
	    }
        } // end of ListView
    } // end of Container
    
    actions: [
        ActionItem {
            title: "Add account"
            ActionBar.placement: ActionBarPlacement.OnBar
            
            onTriggered: {
                //It works fine here
                accountsManager.doSomething();
            }
        }
    ]
} // end of Page

 There could be something wroing in my code, could you help to take a look? or if possible, give me an example. Thanks.

 

Dong

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Is this the same issue as http://supportforums.blackberry.com/t5/Cascades-Development/ListView-datamodel-access/td-p/1864605?   Follow that other thread in case it solves your case.

 

Stuart

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

I reproduced what may be the issue using BatterySample.  Set the context before creating root.

 

In HelloBattery constructor:

 

...this works:

 

        QmlDocument *qml = QmlDocument::create("HelloBattery.qml");
        // bind battery info to the qml context
        qml->setContextProperty("battery", new bb::device::BatteryInfo(this));
        AbstractPane *root = qml->createRootNode<AbstractPane>();
        Application::instance()->setScene(root);

 

...this gives warning

file:///accounts/1000/appdata/com.example.BatterySample.testDev_tterySampleb5ebcfc6/app/native/assets/HelloBattery.qml:130: ReferenceError: Can't find variable: battery
file:///accounts/1000/appdata/com.example.BatterySample.testDev_tterySampleb5ebcfc6/app/native/assets/HelloBattery.qml:65: ReferenceError: Can't find variable: battery

 

        QmlDocument *qml = QmlDocument::create("HelloBattery.qml");
        AbstractPane *root = qml->createRootNode<AbstractPane>();
        qml->setContextProperty("battery", new bb::device::BatteryInfo(this));
        Application::instance()->setScene(root);

Stuart

Please use plain text.
Developer
contryboy
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Hi Smacmartin,

I read it carefully, and I also tried BuckList demo.
I would say I have a different case with BuckList Demo. In BuckList demo, the list data model object is created in QML, and C++ does not have a pointer to the model object. It's like QML created an model object, and QML operate the model object (e.g. delete items)
How ever, in my case, the data Model is created in C++, and I want the delete action is also to be handled in C++. So I need a way to call a method of an object created in C++ from QML.
Please use plain text.
Developer
jan_macura
Posts: 20
Registered: ‎08-02-2012
My Device: -
My Carrier: SW Developer

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Hi,

 

you need to call it via root item ID....in your case ID of your StandardListItem from pasted code is accountItem so: accountItem.ListItem.view.doSomething()

 

this should work

 

Jan

 

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

I still suspect you need to set the context before creating the UI.  Your UI is complaining it doesn't recognize the context.

 

Stuart

Please use plain text.
Developer
jan_macura
Posts: 20
Registered: ‎08-02-2012
My Device: -
My Carrier: SW Developer

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

I set context property at the same time as contryboy did and it is working fine for me..no error messages

Please use plain text.
Developer
contryboy
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

are you also call the context property object's method in the contextActions of a listItem?
Please use plain text.