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: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone
Accepted Solution

How to call a function in c++ from QML?

[ Edited ]

Hi all,

 

In my app i having two qml's and a cpp class.

One of my root qml saying main.qml is a root page, which shows a list. And the row in the list is another qml(Customrowlist). And i have set the contextProperty for main.qml in cpp and i have to call a function in cpp from 

Customrowlist.qml. 

 

I want to call the function deleteAccountData(..,..,..); from Customrowlist.qml, so how can i do that.

 

Below is my code

main.qml

 

        TabbedPane {
            
            //property Page about
            
            id: tabbedPane
            showTabsOnActionBar: true
            
            Tab {
                title: "Manage Accounts"
                onTriggered: {
                    objectAM.fetchAccountData();
                }
                imageSource: "asset:///images/list.png"
                NavigationPane {
                    id: navPane
                    Page {
                        Container {
                            background: back.imagePaint
                            layout: StackLayout {
                            }
                            ImageView {
                                imageSource: "asset:///images/head.png"
                                scalingMethod: ScalingMethod.AspectFit
                                opacity: 1.0
                            }
                            ListView {
                                id:savedaccount
                                objectName: "savedaccount"
                                listItemComponents: [
                                    ListItemComponent {
                                        type: "item"
                                        CustomListRow {
                                        }
                                    }
                                ]
                            }
                        }
                        actions: [
                            ActionItem {
                                id: about
                                title: "About"
                                imageSource: "asset:///images/info.png"
                                ActionBar.placement: ActionBarPlacement.InOverflow
                                attachedObjects: [
                                    ComponentDefinition {
                                        id: aboutPageMA
                                        source: "about.qml"
                                    }
                                ]
                                onTriggered: {
                                    var profilePage = aboutPageMA.createObject();
                                    navPane.push(profilePage);
                                }
                            },
                            ActionItem {
                                id: help
                                title: "Help"
                                imageSource: "asset:///images/help.png"
                                ActionBar.placement: ActionBarPlacement.InOverflow
                                attachedObjects: [
                                    ComponentDefinition {
                                        id: helpPageMA
                                        source: "Help.qml"
                                    }
                                ]
                                onTriggered: {
                                    var profilePage = helpPageMA.createObject();
                                    navPane.push(profilePage);
                                }
                            },
                            ActionItem {
                                id: settings
                                title: "Settings"
                                imageSource: "asset:///images/settings.png"
                                ActionBar.placement: ActionBarPlacement.InOverflow
                                attachedObjects: [
                                    ComponentDefinition {
                                        id: settingsPageMA
                                        source: "Settings.qml"
                                    }
                                ]
                                onTriggered: {
                                    var profilePage = settingsPageMA.createObject();
                                    navPane.push(profilePage);
                                }
                            }
                        ]
                    
                    
                    }
                }
            }
            
            attachedObjects: [
                GroupDataModel {
                    id: feedsDataModel
                    sortingKeys: ["text"]
                    sortedAscending: false
                    grouping: ItemGrouping.None
                },
                DataSource {
                    id: feedsDataSource
                    source: "mydata.json"
                    type: DataSource.Json
                    onDataLoaded: {
                        feedsDataModel.clear();
                        feedsDataModel.insertList(data);
                    }
                    onError: {
                        console.log("Error Occured" + errorMessage);
                    }
                },
                ImagePaintDefinition {
                    id: back
                    repeatPattern: RepeatPattern.XY
                    imageSource: "asset:///images/bg.jpg"
                }
            
            ]
        }

 

 

my Customlistrow.qml

 

import bb.system 1.0
import bb.cascades 1.0
import bb.data 1.0  

      Container {
            layout: AbsoluteLayout {}
            Container{
                id: usr
                layoutProperties: AbsoluteLayoutProperties {
                    positionX: 0.0
                    positionY: 0.0
                }
                background: Color.create ("#ffffff")
                preferredWidth: 768
                preferredHeight:120
                Container{
                    layout: AbsoluteLayout {}
                    ImageView {
                        id:accountimg
                        imageSource: ListItemData.account
                        preferredWidth: 78
                        opacity: 1.0
                        layoutProperties: AbsoluteLayoutProperties {
                            positionX: 10.0
                            positionY: 16.0
                        }
                    }
                    Label {
                        id: username
                        text: ListItemData.username
                        textStyle.base: SystemDefaults.TextStyles.TitleText
                        multiline: true
                        textStyle.color: Color.Black
                        textStyle.textAlign: TextAlign.Left
                        layoutProperties: AbsoluteLayoutProperties {
                            positionX: 110.0
                            positionY: 25.0
                        }
                    }
                    ImageButton {
                        defaultImageSource: "asset:///images/delete.png"
                        pressedImageSource: "asset:///images/deletepressed.png"
                        preferredWidth: 78
                        opacity: 1.0
                        layoutProperties: AbsoluteLayoutProperties {
                            positionX: 680.0
                            positionY: 16.0
                        }
                        attachedObjects: [
                            SystemToast {
                                id: myQmlToast
                                body: username.text
                            }
                        ]
                        onClicked: {
                            myQmlToast.show()
                            objectAM.deleteAccountData(username.text,password.text,"asset:///images/twitter.png");
                        }
                    }
                    Divider {
                        layoutProperties: AbsoluteLayoutProperties {
                            positionX: 0.0
                            positionY: 118.0
                        }
                    }
                
                }
                
                gestureHandlers: [
                    LongPressHandler {
                        onLongPressed: {
                            id:finalx.play();
                            secondcontainer.visible = true;
                        }
                    }
                ]
                
                onTouch: {
                    if (event.isUp ()){
                        initialx.play();
                        secondcontainer.visible = false;
                    }
                }
                
                animations: [
                    FadeTransition {
                        id:finalx
                        toOpacity: 0
                        duration: 300
                    },
                    FadeTransition {
                        id:initialx
                        toOpacity: 1
                        duration: 300
                    }
                ]
            }
        }

 

 

 

in my cpp

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("objectAM",this);
 
void AccountManager::deleteAccountData(QString user,QString pass,QString imgSource){
SystemToast *toast = new SystemToast();
toast->setBody("Entered delete account!!!");
toast->show();
}

 

 

Developer
Posts: 18
Registered: ‎04-09-2013
My Device: Z10
My Carrier: Vodafone

Re: How to call a function in c++ from QML?

Please declare that function as Q_INVOKABLE in headerfileand you can easily call it.

deleteAccountData(username.text,password.

text,"asset:///images/twitter.png");

Trusted Contributor
Posts: 134
Registered: ‎04-12-2013
My Device: BB Z10
My Carrier: Telenor

Re: How to call a function in c++ from QML?

Hi,

 

You can just call the function from ur QML file simply. But make sure you declare the function as Q_INVOKABLE in the hpp file. 

 

Q_INVOKABLE (return type)  deleteAccountData(..,..,..);

 

in QML file call the function ..

 

objectAM.deleteAccountData();

 

hope this helps..

 

thanks,,

Please like this if you are answered.
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: How to call a function in c++ from QML?

I have already declred the function as Q_INVOKABLE in hpp class.

Trusted Contributor
Posts: 134
Registered: ‎04-12-2013
My Device: BB Z10
My Carrier: Telenor

Re: How to call a function in c++ from QML?

Then calling the function from the qml should work.. what is the issue you facing now?

Please like this if you are answered.
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: How to call a function in c++ from QML?

deleteAccountData() is not calling...
Developer
Posts: 18
Registered: ‎04-09-2013
My Device: Z10
My Carrier: Vodafone

Re: How to call a function in c++ from QML?

I got your problem .Your CustomListrow is not getting the reference to your context "objectAM".

 

Please update your main.qml with following...function and store the reference to global object then from your customListRow call as  "Qt.objectAM.deleteAccount();

 

In main.Qml on creation completed

onCreationCompleted: {
        Qt.objectAM=objectAM;
    }

 

In CustomListRow..

onClicked: {
                   // myQmlToast.show()
                    //objectAM.deleteAccountData(username.text, password.text, "asset:///images/twitter.png");
                    Qt.objectAM.deleteAccountData("Me","pwd", "asset:///images/twitter.png");
                }

 

Hope it will work.

Thanks

Developer
Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE
My Carrier: AirTel

Re: How to call a function in c++ from QML?

Hi,

 

Agree with Shashadhar . if not work check this

 

http://supportforums.blackberry.com/t5/Cascades-Development/ContextActions-inside-ListItem-can-not-a...

 

accountItem.ListItem.view.myAction();

 

:Devil2: