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

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

[ Edited ]

I created a minimum project to demostrate my problem, how ever, the reply does not allow to attach files, so i post my code here, hope it can express my problem better:

 

main.qml:

import bb.cascades 1.0

Page {
    content: Container {        
         
        ListView {
            id: accountListView
            objectName: "accountsListView"             

            listItemComponents: [
                ListItemComponent {
                    type: "listItem"
                    StandardListItem{
                        id: accountItem
                        title: ListItemData.username
                    contextActions: [
                            ActionSet {
                                ActionItem {
                                    title: "myAction"
                                    onTriggered: {
                                        //it does not work here.
                                        app.myAction();
                                    }
                                }
                            }
                        ]
                    }
                } 
            ]
            
            function itemType(data, indexPath) {
                return "listItem";
            }
            
        } // end of ListView
    } // end of Container
    
    actions: [
        ActionItem {
            title: "myAction"
            onTriggered: {
                //it works here.
                app.myAction();
            }
        }
    ]
} 

 app.hpp:

#ifndef APP_H
#define APP_H

#include <QObject>

/*!
 * @brief Application GUI object
 */
class App : public QObject
{
    Q_OBJECT
public:
    App();
    Q_INVOKABLE void myAction();
};

#endif // ifndef APP_H

 app.cpp:

#include "app.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/QListDataModel>
#include <bb/cascades/ListView>

using namespace bb::cascades;

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    qml->setContextProperty("app", this);
    AbstractPane *root = qml->createRootNode<AbstractPane>();

    QListDataModel<QVariantMap>* listDataModel = new QListDataModel<QVariantMap>();
    QVariantMap map1;
    map1["username"] = "username1";
    listDataModel->append(map1);
    QVariantMap map2;
    map2["username"] = "username1";
    listDataModel->append(map2);
    ListView* accountsListView = root->findChild<ListView*>("accountsListView");
    accountsListView->setDataModel(listDataModel);

    Application::setScene(root);
}


void App::myAction()
{
	qDebug("myAction() is called...");
}

 

 main.cpp (no change):

#include "app.hpp"

#include <bb/cascades/Application>

#include <QLocale>
#include <QTranslator>

using ::bb::cascades::Application;

int main(int argc, char **argv)
{
    //-- this is where the server is started etc
    Application app(argc, argv);
    
    //-- localization support
    QTranslator translator;
    QString locale_string = QLocale().name();
    QString filename = QString( "ListContextActionTest_%1" ).arg( locale_string );
    if (translator.load(filename, "app/native/qm")) {
        app.installTranslator( &translator );
    }
    
    App mainApp;
    
    //-- we complete the transaction started in the app constructor and start the client event loop here
    return Application::exec();
    //-- when loop is exited the Application deletes the scene which deletes all its children (per qt rules for children)
}

 

Highlighted
Contributor
Posts: 20
Registered: ‎08-02-2012
My Device: -
My Carrier: SW Developer

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Please check the changes in bold...this works fine for me

 

import bb.cascades 1.0

Page {
content: Container {

ListView {
id: accountListView
objectName: "accountsListView"

listItemComponents: [
ListItemComponent {
type: "listItem"
StandardListItem{
id: accountItem
title: ListItemData.username
contextActions: [
ActionSet {
ActionItem {
title: "myAction"
onTriggered: {
//it does not work here.
accountItem.ListItem.view.myAction();
}
}
}
]
}
}
]

function myAction() {
app.myAction();
}

function itemType(data, indexPath) {
return "listItem";
}

} // end of ListView
} // end of Container

actions: [
ActionItem {
title: "myAction"
onTriggered: {
//it works here.
app.myAction();
}
}
]
}

Developer
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Thanks very much, it works.
Contributor
Posts: 26
Registered: ‎08-25-2011
My Device: Z10
My Carrier: Bell

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

After five hours it's just about time for me to find. Thank you!
New Developer
Posts: 168
Registered: ‎04-12-2010
My Device: Storm
My Carrier: Verizon

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

[ Edited ]

Guys this has been a while ago but  I have the very same issue without a solution yet. Same as the original poster here I dont get it to work in a ContextAction, I get the menus to pop but when I click on an ActionItem nothing happens, I tried the suggestion above which is the same as the Bucket sample, lots of permutations but still I don't get the menu to call the ontriggered section,  My code is just about the same as of the original poster here I have a custom ListItemComponent where I'm trying t oget the menus to work.

 

the samples here and on the Bucket sample they use a StandardListItem which I'm not using in my customitemcomponent is that  it???? it only works with a Standard entry???? If this is so .. then it is not availabe for a CustomItem???

 

Any things to look for???

Contributor
Posts: 26
Registered: ‎08-25-2011
My Device: Z10
My Carrier: Bell

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

This is working for me.

 

 

ListView {
    id: wineList
    dataModel: ArrayDataModel {
        id: myModel
    }
    listItemComponents: [
        ListItemComponent {
            Container {
                id: wineItem
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                Label {
                    text: ListItemData.name
                }
                contextActions: [
                    ActionSet {
                        title: "Contact"
                        ActionItem {
                            title: "Send an Email"
                            imageSource: "asset:///images/add.png"
                            onTriggered: {
                                wineItem.ListItem.view.myAction();
                            }

                        }
                    } // end of ActionSet
                ]
            } // Container

        }
    ]
    onActivationChanged: {
        chosenItem = dataModel.data(indexPath);
    }

    function myAction() {

    }
}
New Developer
Posts: 168
Registered: ‎04-12-2010
My Device: Storm
My Carrier: Verizon

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

Bingo!!!! cool man 

 

From all of the permutations I tried the one on your sample is the one I didn't .... e.g. put the id inside the main Container all of the samples I've seen they used a StandardItem and put the id outside,  we always know better after the fact I see it is logical.

Regular Contributor
Posts: 51
Registered: ‎08-07-2013
My Device: blackberry z10
My Carrier: asif

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

We cannot use standard list item we can use the customic list listview

for example

hello sir
i can access button in a listview
for example
ListView {
translationY: 00
dataModel: dataModelList
listItemComponents: [
ListItemComponent {
type: "item"
Container {
Label {
text: "Category:"
}
Label {
text: ListItemData.cat_name
}
ImageButton {
id: view_details_btn
defaultImageSource: "asset:///coupon%20list/viewdetail_button.png"
pressedImageSource: "asset:///coupon%20list/viewdetailhover_button.png"
onClicked: {
}
}
}
]
onTriggered: {
var showDetail = productDetailPageID.createObject();
showDetail.myProfileData=dataModel.data(indexPath);
navigationPane.push(showDetail);
}
}
i can access this button.....

New Developer
Posts: 5
Registered: ‎04-17-2014
My Device: Z10
My Carrier: TIM

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

[ Edited ]

Hi,
Maybe there is an error in the second qml example

doSometing() instead doSomething()

Smiley Wink

New Developer
Posts: 14
Registered: ‎12-15-2015
My Device: Blackberry Z10
My Carrier: Lebara

Re: ContextActions inside ListItem can not access ContextProperty in QMLDocument

[ Edited ]

Hello,

 

How can I achieve that with this code:

ListView {
            accessibility.name: "myLVCapitulos"
            id: myLVCapitulos
            dataModel: gdmVersiculos
            listItemComponents: ListItemComponent {
                type: "item"
                Container {
                    id: cMain
                    layout: StackLayout {
                    
                    }
                    
                    preferredWidth: Infinity
                    preferredHeight: 160;
                    topMargin: 10.0
                    bottomMargin: 10.0
                    
                                       
                    Container {
                        id: cOutterChat
                        accessibility.name: "cOutterChat"
                        preferredWidth: Infinity
                        layout: DockLayout {}
                        leftPadding: (ListItemData.Cita == "1") ? 15.0 : 40.0
                        rightPadding: (ListItemData.Cita == "1") ? 40.0 : 15.0
                        //leftPadding: 15.0
                        //rightPadding:10.0
                    
                       ImageView {
                            id: cOutterChat2
                            accessibility.name: "cOutterChat2"
                            imageSource:  (ListItemData.Cita == "1") ? "asset:///images/bubblegod.amd" : "asset:///images/bubbleword.amd"
                            horizontalAlignment: HorizontalAlignment.Fill
                            verticalAlignment: VerticalAlignment.Fill
                        }
                        Container {
                            id: cInnerBubble
                            leftPadding: 20.0
                            topPadding: 20.0
                            rightPadding: 20.0
                            bottomPadding: 30.0
                           
                            Label {
                                minHeight: (Math.round(String(ListItemData.Versiculo.toString()).length / 55) + 1) * 50
                                text: ListItemData.Versiculo
                                textStyle {
                                    color: Color.White
                                    fontFamily: "Slate Pro"
                                    fontSize:  FontSize.Small
                                    textAlign: TextAlign.Left
                                }
                                preferredWidth: cOutterChat.preferredWidth - 50.0
                                textFormat: TextFormat.Auto
                                multiline: true
                                contextActions: [
                                    // Add a set of four actions to the
                                    // context menu for a list item
                                    ActionSet {
                                        title: "Acciones"
                                        ActionItem {
                                            title: "Copiar Versículo"
                                            imageSource: "asset:///images/copy.png"
                                            onTriggered: {
//THIS WORKS
cMain.ListItem.view.copyAction(ListItemData.Versiculo); //HERE DOESNT WORK-- //cOutterChat.ListItem.view.copyAction(ListItemData.Versiculo); } } ActionItem { title: "Agregar Favoritos" imageSource: "asset:///images/favoriteNO.png" onTriggered: { } } } // end of ActionSet ] } Label { text: "Versículo " + ListItemData.IdVersiculo textStyle.color: Color.White preferredWidth: cMain.preferredWidth - 20.0 multiline: false textStyle.fontFamily: "Georgia" textStyle.fontSize: FontSize.XSmall rightMargin: 40.0 textStyle.textAlign: TextAlign.Right translationX: 5.0 } } } } } //HERE IS THE FUNCTION function copyAction(text) {
//THIS WORKS
app.CopyText("text: " + text);
//THIS NO WORKS //app.CopyText("text: " + ListItemData.Versiculo); } attachedObjects: [ GroupDataModel { id: gdmVersiculos //sortingKeys: ["IdVersiculo"] }, DataSource { id: dtVersiculos // Load the data from an SQL database, based on a specific query source: "asset:///sql/MyBiblia.db" onDataLoaded: { gdmVersiculos.clear(); gdmVersiculos.insertList(data); } } ] }

 

UPDATE:

 

I Edited this code and Now works perfectly.

 

I hope it can help next person who is looking for the solution for the same problem.

 

THANKS TO EVERYBODY Smiley Happy