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
Posts: 104
Registered: ‎04-15-2012
My Device: BlackBerry PlayBook with OS 2.0
My Carrier: N/A

Get ListItem upon clicking a ContextAction from C++

Hi,

 

I have a QML/C++ project in which the ListView's context actions are setup as in the first code block below:

http://supportforums.blackberry.com/t5/Cascades-Development/HOWTO-Use-context-action-item-with-ListV...

 

And the custom slots assigned to the triggered() signals of each of the ListItems as in the 2nd code block (in the above link).

 

So my question is, how do I get the item data from which the context action was invoked upon? For example, say I have a list of food items:

 - Bacon

 - Eggs

 - Potato

 

And the list has a context menu with the items "Eat", "Cook", and etc. and I have connected the "Eat"'s triggered() signal to a custom slot called "eatTheFood()". And from that slot, how do I know which food item was selected to eat?

My BlackBerry 10 apps: Roshnal Lihinikadu
Lots more coming up!
Developer
Posts: 43
Registered: ‎12-16-2011
My Device: Torch 9800 & Playbook
My Carrier: Three

Re: Get ListItem upon clicking a ContextAction from C++

Hi Roshnal, I have battled with this situation myself. A solution (which i didn't stick with) is to capture the selected ListView's selectionChanged() signal and store the qvariant which points to the item in the list the user selected to trigget the context menu. Then when an action in the context menu is triggered you can use the qvariant to get the data. However the issue I had with this solution is that a context menu will be shown, even if the user has not selected a ListItem. The context menu will be shown on a long press on the ListView, so it is possible, if the user long press's between ListItem's (if there is a gap between list items) that you will get a context menu, but never have received the ListView's selectionChanged() signal. You can also test this if you have a listview which only has a couple of items and is not filled by them, just long press after the last list item. Since a context menu can be added to any container, I decided to add a context menu to my custom ListItem's. This meant that a context menu is only ever shown when my listitem is selected. This also meant that I could give my listitems the ability to send out a signal indicating which action was selected from the context menu along with the listitems ID. To find the item in the datamodel is then fairly trivial. A concern I had with this solution was performance and memory footprint since each list item would have its own context menu, but I can now say that I have seen no degridation of the performance in my app. However, I have used this technique on lists that are not particulary big. I have yet to find the 'ideal' solution.
regards

Daniel - Neurocode
If my posts help, please click the Like button.

gymChronicles - helping you to keep motivated
Developer
Posts: 43
Registered: ‎12-16-2011
My Device: Torch 9800 & Playbook
My Carrier: Three

Re: Get ListItem upon clicking a ContextAction from C++

Sorry about formatting ^^^. It was stripped from my post and I seem unable to edit it, at this time.
regards

Daniel - Neurocode
If my posts help, please click the Like button.

gymChronicles - helping you to keep motivated
Developer
Posts: 104
Registered: ‎04-15-2012
My Device: BlackBerry PlayBook with OS 2.0
My Carrier: N/A

Re: Get ListItem upon clicking a ContextAction from C++

Hi Daniel,

Thanks a lot for your reply- got the idea... I will try it out and get back to you.
My BlackBerry 10 apps: Roshnal Lihinikadu
Lots more coming up!
Developer
Posts: 104
Registered: ‎04-15-2012
My Device: BlackBerry PlayBook with OS 2.0
My Carrier: N/A

Re: Get ListItem upon clicking a ContextAction from C++

It worked... But yeah, the context menu shows up when you long-press after the last list item. And your second solution (adding the contextActions inside the ListItem (I use StandardListItem)) resolves this issue, then the C++ code in the link doesn't work. I'm completely handing every signals/slots in my C++ code, so I think I can't send a signal from QML to be caught in C++?

My BlackBerry 10 apps: Roshnal Lihinikadu
Lots more coming up!