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: 49
Registered: ‎05-02-2013
My Device: Z 10
My Carrier: airtel

Can't Access Variable inside QML

import bb.cascades 1.0
import bb.system 1.0
Page {
    id: pp
    property int num          
    ListView {
        dataModel: XmlDataModel {               
          source: "model/animals.xml"
            }
            listItemComponents: [
                ListItemComponent {                    
                    type: "listItem"
                    Container {
                        layout: DockLayout {
                        }                     
                        ImageView {
                            imageSource: "asset:///images/" + ListItemData.title + ".png"
                           }
                    
                        ImageButton {                           
                            defaultImageSource: "asset:///images/twitterb.png"
                            onClicked: {  
                                num=5;
                                console.log("value of num : "+num)
//HERE IS THE PROBLEM , CAN'T ACESS VARIABLE "num" .
//Error: Invalid write to global property "num"

                        }
                        }
                    }
                } // end of second ListItemComponent
            ]

        }
    }

 In the above code i can't access yhe variable "num" insde button click .

the error is

Error: Invalid write to global property "num"
pls help me.

 

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Can't Access Variable inside QML

a list item component cannot access the properties of the outside qml.
(it seems this is the most asked question of the last few days)
you can access the listview itself from it or use a global property, for example via the Qt.xxx mechanism.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 167
Registered: ‎04-12-2010
My Device: Storm
My Carrier: Verizon

Re: Can't Access Variable inside QML



// //// main QML ... import QtQuick 1.0 ... ... I'm getting some signal from C++ and wanted to set the Dialog label , Connections { target: _myWackyApp.send_msg // C++ signature is void start_Waity(const QString &with_this_msg); onStart_Waity: { ... Qt.waityMessage.text= with_this_msg; ... } ..... ///// another QML file where you want to access stuff /////// Dialog { id: _funnyDialog // make this inner contrl accessible from outside onCreationCompleted: { Qt.waityMessage = waityMessage; } .... Label { id:waityMessage ... blah blah blah

 

This is how I did it