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
smiley
Posts: 1,468
Registered: ‎07-14-2008
My Device: Z10

How to remove and add ActionItems to Context menu?

So I have a Context Actions added to a ListView with 3 action items.

 

When the user selects an item from the listview I want to remove one item from the ActionSet.

I have tried different ways in C++ and QML and I couldn't remove the item.

 

here is my Listview control

 

                listItemComponents: [
                    ListItemComponent {
                        type: "item"

                        Container {
                            id: itemRoot
                            
                            layout: DockLayout {
                            }

                            contextActions: [
                                ActionSet {
                                    id: action_set
                                    title: "Select Action"

                                    objectName: "action_set"
                                    ActionItem {
                                        title: "Edit"
                                        onTriggered: {
                                            // do something
                                        }
                                    }

                                    ActionItem {
                                    	id: action_clear_msg
                                        title: "Clear Flag"
                                        onTriggered: {
                                            // do something
                                        }
                                    }

                                    DeleteActionItem {
                                        title: "Delete"
                                                                                                              onTriggered: {
                                         // do something
                                                                      }
                                    }

                                }
                            ]

                        } 
                    }
                ] 

 I want to remove Clear Flag action item.  How would I do this in C++?

I have tried removing the action item from the action set and it didn't work.

 

Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: How to remove and add ActionItems to Context menu?

I don't know how to do this declarative way, ActionItems don't have 'visible' property. I usually recreate all actions in C++ code:

 

void CalculatorPage::recreateActions()
{       
        removeAllActions();
                                                     
        // -----------                                  
        // Copy Result                     
        // -----------                                  
        if (showCopy_)
        {                                               
                ActionItem *copyAction = ActionItem::create()
                                .imageSource(QUrl("asset:///images/iconsbb/ic_copy.png"))
                                .title("Copy");
                addAction(copyAction, ActionBarPlacement::OnBar);
                QObject::connect(copyAction, SIGNAL(triggered()),
                                                 this, SLOT(copy_triggered()));
        }

        etc

This can probably be done in QML as well using JavaScript: ComponentDefinition to declare actions / createObject() to create them / addAction() to add them.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
smiley
Posts: 1,468
Registered: ‎07-14-2008
My Device: Z10

Re: How to remove and add ActionItems to Context menu?

Thanks.  Kinda late since I have all the code in QML.

 

Please use plain text.
Developer
greenmr
Posts: 882
Registered: ‎03-20-2013
My Device: Red LE Developer Z10

Re: How to remove and add ActionItems to Context menu?

Are you saying this doesn't work?

 

onTriggered: {
   action_set.remove(action_clear_msg);
}

 


smiley wrote:

So I have a Context Actions added to a ListView with 3 action items.

 

When the user selects an item from the listview I want to remove one item from the ActionSet.

I have tried different ways in C++ and QML and I couldn't remove the item.

 

here is my Listview control

 

                listItemComponents: [
                    ListItemComponent {
                        type: "item"

                        Container {
                            id: itemRoot
                            
                            layout: DockLayout {
                            }

                            contextActions: [
                                ActionSet {
                                    id: action_set
                                    title: "Select Action"

                                    objectName: "action_set"
                                    ActionItem {
                                        title: "Edit"
                                        onTriggered: {
                                            // do something
                                        }
                                    }

                                    ActionItem {
                                    	id: action_clear_msg
                                        title: "Clear Flag"
                                        onTriggered: {
                                            // do something
                                        }
                                    }

                                    DeleteActionItem {
                                        title: "Delete"
                                                                                                              onTriggered: {
                                         // do something
                                                                      }
                                    }

                                }
                            ]

                        } 
                    }
                ] 

 I want to remove Clear Flag action item.  How would I do this in C++?

I have tried removing the action item from the action set and it didn't work.

 






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
smiley
Posts: 1,468
Registered: ‎07-14-2008
My Device: Z10

Re: How to remove and add ActionItems to Context menu?


greenmr wrote:

Are you saying this doesn't work?

 

onTriggered: {
   action_set.remove(action_clear_msg);
}

 

I'm getting  in

 

                onSelectionChanged: {
                    action_set.remove(action_clear_msg)
                }

 

asset:///main.qml:152: ReferenceError: Can't find variable: action_set

 

Please use plain text.
Developer
greenmr
Posts: 882
Registered: ‎03-20-2013
My Device: Red LE Developer Z10

Re: How to remove and add ActionItems to Context menu?

Well, that looks like action_set is not in scope. The problem is that ListItemComponents are not in the same scope as the object you declare them in since they are created and destroyed as needed by the ListView at runtime. To get access to the actual ListItemComponent from the ListView::selectionChanged slot you have to use the indexPath parameter to find the selected item in the ListView dataModel. Even then, I'm not sure that you will have access that way to the ListItemComponent or just to the item data.

 

What you are trying to do is fairly easy with ListItemProvider in C++, but very tricky with ListItemComponent in QML/JavaScript. In my latest release I switched from QML and ListItemComponent to C++ and ListItemProvider and accessing and manipulating the items is MUCH easier now.

 


smiley wrote:

greenmr wrote:

Are you saying this doesn't work?

 

onTriggered: {
   action_set.remove(action_clear_msg);
}

 

I'm getting  in

 

                onSelectionChanged: {
                    action_set.remove(action_clear_msg)
                }

 

asset:///main.qml:152: ReferenceError: Can't find variable: action_set

 






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
Ebscer
Posts: 867
Registered: ‎08-31-2009
My Device: 9530, 9630, 9800, 8530, 9900, 9810, 9930, PlayBook, Dev Alpha

Re: How to remove and add ActionItems to Context menu?

You can do this if you are a bit less declaritive and create the ActionItem as an attached object. Then just manually add and remove it from the action bar.

 

	Page
	{
	    id: mainPage
	    attachedObjects:
            [
		    ActionItem
		    {
		        id: helpAction
		        title: "Help"
		        ActionBar.placement: ActionBarPlacement.OnBar
		        imageSource: "asset:///images/help.png"
		        onTriggered:
		        {
		            howToDialog.start()
		        }
		    }
            ]
            onCreationCompleted:
            {
		    mainPage.showActions()
            }
            function showActions()
            {
		    mainPage.addAction(helpAction)
            }
            function hideActions()
            {
		    mainPage.removeAction(helpAction)
            }
	}

 


Read my thoughts on BlackBerry Development at news.ebscer.com
Please use plain text.
Developer
greenmr
Posts: 882
Registered: ‎03-20-2013
My Device: Red LE Developer Z10

Re: How to remove and add ActionItems to Context menu?

Yes, I've done that in my app too, but that won't work in this case because the ActionItem in question is not a nice simple one on a page like your example, but is on a ListItemComponent and the OP is trying to hide/show the button from the onSelectionChanged slot of the parent ListView. The ActionItem in question is not directly  visible from the ListView slot.

 


Ebscer wrote:

You can do this if you are a bit less declaritive and create the ActionItem as an attached object. Then just manually add and remove it from the action bar.

 

	Page
	{
	    id: mainPage
	    attachedObjects:
            [
		    ActionItem
		    {
		        id: helpAction
		        title: "Help"
		        ActionBar.placement: ActionBarPlacement.OnBar
		        imageSource: "asset:///images/help.png"
		        onTriggered:
		        {
		            howToDialog.start()
		        }
		    }
            ]
            onCreationCompleted:
            {
		    mainPage.showActions()
            }
            function showActions()
            {
		    mainPage.addAction(helpAction)
            }
            function hideActions()
            {
		    mainPage.removeAction(helpAction)
            }
	}

 






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.