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
Contributor
Posts: 34
Registered: ‎12-20-2010
My Device: DevAlpha Beta 2
My Carrier: Orange
Accepted Solution

Cannot access (outside) QML elements from ListItemComponent

Hello,

 

I have a problem accessing variables from a ListItemComponent in my QML.

My QML is a Container, containing a ListView with several ListItemComponents.

I set the list data model as a contextProperty to my QML :

qml->setContextProperty("_dataModel", homeDataModel);

My data model has Q_INVOKABLE that I want to call from my QML.

The problem is that, in ListItemComponent, I cannot access neither "_dataModel" nor all the elements that are outside the component itself.

 

I found a solution that would be using Qt scope such as :

onCreationCompleted: { 

        Qt.homePage = homePage;

        Qt.homeList = homeList; 

}

 

 

Using that, I could access external variables from my ListItemComponent. The problem is that the Qt scope is too big. Indeed, if I create several instances of the Pages using the QML, Qt.homePage/Qt.homeList will have the value given by the latest Page instance. This is not a good solution for me since I use it to access the dataModel of the list and the dataModel has a NavigationPane associated with.

 

Anyway, do you know any other solutions to access my data model from my ListItemComponent?

 

Thank you

BlackBerry Development Advisor (Retired)
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Cannot access (outside) QML elements from ListItemComponent

Is this a cascades application or a Qt application?

 

Stuart

Contributor
Posts: 34
Registered: ‎12-20-2010
My Device: DevAlpha Beta 2
My Carrier: Orange

Re: Cannot access (outside) QML elements from ListItemComponent

This is an application for BB10 using the cascade framework.

So it's using both cascade and Qt.

BlackBerry Development Advisor (Retired)
Posts: 138
Registered: ‎05-11-2012
My Device: Developer
My Carrier: Developer

Re: Cannot access (outside) QML elements from ListItemComponent

Have you tried using attachedObjects to connect to your events?

 

Samples like the bucketlist app may help you.

 

Graham

New Contributor
Posts: 7
Registered: ‎11-22-2010
My Device: Not Specified

Re: Cannot access (outside) QML elements from ListItemComponent

I just hit this same issue today... 

 

You can get a handle from the list item back to the view, and from there, get anything you need... 

 

In my list item component:

                listItemComponents: [
                    ListItemComponent {
                        Container {
                            id: itemRoot
...

 You can then refer to item root, and ultimately the view... Here's a button that is within my list item... Dumping a bunch of things, and calling trigger on the list view... 

 

                           Button {
                                text: qsTr("Go!")
                                horizontalAlignment: HorizontalAlignment.Center
                                onClicked: {
                                    console.debug("ListItemData: " + ListItemData);
                                    console.debug("itemRoot: " + itemRoot);
                                    console.debug("itemRoot.ListItem: " + itemRoot.ListItem);
                                    console.debug("itemRoot.ListItem.view: " + itemRoot.ListItem.view);
                                    console.debug("itemRoot.ListItem.view: " + itemRoot.ListItem.view.dataModel);
                                    console.debug("itemRoot.ListItem.view.trigger: " + itemRoot.ListItem.view.triggered(ListItemData.indexPath));
...

 

BlackBerry Development Advisor (Retired)
Posts: 138
Registered: ‎05-11-2012
My Device: Developer
My Carrier: Developer

Re: Cannot access (outside) QML elements from ListItemComponent

In many cases we have fixed problems by using aliases to make properties visible such as -

 

property alias title: addBar.title
property alias hintText: itemText.hintText
property alias text: itemText.text

 


Graham

Developer
Posts: 419
Registered: ‎04-10-2011
My Device: Z10 LE & PlayBook

Re: Cannot access (outside) QML elements from ListItemComponent

Thanks a lot! That was exactly what I was looking for.

Is it a bug that you can't access objects, functions and properties from the outside or did RIM intend to make the ListItemComponents "live" somewhere else?
-----------------------------------------------------------------------------
Check out my apps in the BlackBerry World
Visit my developer blogs /dev/alopix and /home/alopix
BBM Channel: C0047B612