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
BojanKogoj
Posts: 46
Registered: ‎01-08-2013
My Device: Z10 LE
My Carrier: SiMobil
Accepted Solution

Access cpp function from ListView in QML

[ Edited ]
ListView {
            id: listView
            dataModel: dataModel
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    StandardListItem {
                        title: ListItemData.title
                        description: app.formatDate(ListItemData.pubDate);                      
                    }
                }
         ]
}

 

and in cpp

 

QString ApplicationUI::formatDate(QString date) {
	return "bla";
}

 Function call from outside ListView works well, but I can't get access to any function defined outside ListView!

 

In this case, I get asset:///blabla.qml:16: ReferenceError: Can't find variable: app

How can I call it?

Please use plain text.
Developer
oniongarlic
Posts: 46
Registered: ‎10-19-2012
My Device: Playbook, BB10DevAlphaB
My Carrier: Sonera, Saunalahti

Re: Access cpp function from ListView in QML

For some odd reason the listItemComponents are outside of the normal code flow. But you can work around it. Give your listItemComponent a name/id. Then you can reference trough the the parent ListView. Something like this should work:

 

StandardListItem {
 id: mscurli 
 title: ListItemData.OriginalTitle;
 description: ListItemData.TheatreAndAuditorium
 status: mscurli.ListItem.view.getShowTime(ListItemData.dttmShowStart);
} 

I used a simple helper function defined inside my ListView to get to the C++ code:

 

function getShowTime(str) {
 return _myapp.formatTime(str);
}
Please use plain text.
Developer
BojanKogoj
Posts: 46
Registered: ‎01-08-2013
My Device: Z10 LE
My Carrier: SiMobil

Re: Access cpp function from ListView in QML

Thank you, that did the trick! For anyone else looking, this is what worked for me:

 

ListView {
            id: listView
            dataModel: dataModel
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    StandardListItem {
                        id: listItem
                        title: ListItemData.title
                        description: listItem.ListItem.view.formatDate(ListItemData.pubDate)
                    }
                }
            ]
            function formatDate(date) {
                return app.formatDate(date);
            }
}

 

Please use plain text.