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
Dev_Brooke
Posts: 17
Registered: ‎06-19-2013
My Device: BlackBerry Z10
My Carrier: O2

Multiple Data Models

Hello all,

 

I have a complicated problem with qml which I would appreciate a little help with.

 

I have a page in qml which only contains a Vertical ListView - this Vertical ListView is the only content of the page. Within this ListViews ListItemComponent I am adding other Horizontal ListViews to display images horizontally.

 

I am appending 10 rows to Vertical ListView which means qml is creating 10 Horizontal ListViews which means another 10 data models - id: hoz_list_model. 

 

When Horizontal ListViews listitemcomponet starts to display these ListViews in qml I access the object appended to its datamodel using ListItem.data and then access the url of the object and also a counter variable. Every Horizontal ListViews has an object appended to it which has a url for an image and a counter of 0 - 9.

 

The problem is that only objects with counter 0, 1, 2, 5 or 9 are the only ones I ever get access to when using ListItem.data in Horizontal ListView ListItemComponent, where do rows containing hoz_list 3, 4, 6, 7, and 8 go? I print to the console each time a url is requested for an image in the Horizontal ListView and thats what I find only 5 of the Horizontal ListViews are displayed using ListItemComponent.

 

Also the Horizontal ListViews start appearing in wrong rows of the Vertical ListView - it get's messed up.

 

Is it because there are 10 datamodels all with the same id created - possible overwriting or should that not matter? The datamodels are initialised in the listitemcomponent of main_list when a hoz_list  is created and it signals onCreationComplete.

 

Should it be possible to initialise this many datamodels and can anybody explain what might be happening?

Please use plain text.
Contributor
Dev_Brooke
Posts: 17
Registered: ‎06-19-2013
My Device: BlackBerry Z10
My Carrier: O2

Re: Multiple Data Models

[ Edited ]

This is the ListView code for Horizontal Lists

 

ListView {
            property variant path
            preferredHeight: 700
            layout: StackListLayout {
                onCreationCompleted: {
                    hozListModel.setCounter(Qt.counter)
                    var i = mainitemroot.ListItem.data.attachmentCount;
                    for (var y = 0; y < i; y ++) //loop to get images for this horizontal list
                    {
                        var xx = mainitemroot.ListItem.data.picture;
                        if (xx.length > 0) {
                            hozListModel.appendData([
                                    {
                                        "counter": "" + Qt.counter,
                                        "url": "" + xx,
                                        "name": "" + mainitemroot.ListItem.data.fromz
                                    } ]);
                            hozlist.path = xx;
                            Qt.counter ++;
                        } else {
                            hozListModel.appendData([
                                    {
                                        "counter": "" + Qt.counter,
                                        "url": "" + "empty",
                                        "name": "empty"
                                    } ]);
                            hozlist.path = xx;
                            Qt.counter ++;
                        }
                    
                    }
                    console.log(Qt.counter + " is counter for object inserted");
                }
                orientation: LayoutOrientation.LeftToRight
            }
            id: hozlist
            dataModel: ObjectsDataModelz {
                id: hozListModel
            
            }
            
            listItemComponents: [
                ListItemComponent {
                    
                    id: myComponent
                    
                    Container {
                        id: itemroot
                        ListItem.onDataChanged: {
                            //TODO this line is supposed to detect ListView recycling cells
                        
                        }
                        
                        visible: {
                            console.log("itemrootcontiner setting visible");
                            return true; //ListItemData.picture.length > 0;
                        }
                        
                        background: Color.create("#ff0000")
                        layout: StackLayout {
                            orientation: LayoutOrientation.LeftToRight
                        
                        }
                        
                        WebImageView {
                            visible: {
                                console.log("hozimg setting visible");
                                return true;
                            }
                            
                            id: hozimg
                            preferredWidth: 700
                            preferredHeight: 700
                            
                            url: {
                                
                                console.log("counter hoz list=" + itemroot.ListItem.data["counter"]);
                                console.log("url for image in hoz list=" + itemroot.ListItem.data["name"]);
                                return itemroot.ListItem.data["url"];
                            }
                        
                        }
                    }
                
                }
            ]
        
        }

 

Please use plain text.
Contributor
Dev_Brooke
Posts: 17
Registered: ‎06-19-2013
My Device: BlackBerry Z10
My Carrier: O2

Re: Multiple Data Models

bump

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

Re: Multiple Data Models

Hi,
DataModels should work correctly regardless of their count, there are no dependencies between them.

I suspect the problem is in initialization code. In inner ListView you're relying on creationCompleted signal to fill the model with data, but this signal is emitted only when outer ListItemComponents are constructed for first time. Also, the order of their construction is not guaranteed. This signal won't be emitted when the components are reused.

Using a global counter (Qt.counter) will also work incorrectly because only visible ListItemComponents are initially created, not all of them.

If you want to create inner models dynamically when entries from outer model are shown, try to capture item reuse and regenerate the inner model at that moment. I think this can be done with the help of onDataChanged, but it can be tricky.

Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
Dev_Brooke
Posts: 17
Registered: ‎06-19-2013
My Device: BlackBerry Z10
My Carrier: O2

Re: Multiple Data Models

Thanks for your reply.

 

I had noticed that the order of how things were getting done wasn't in the order id expected and suspected this may be a problem but I couldn't find anywhere else to put what i have put in onCreationComplete. And when I gave it thought I just assumed if I put something in a model, when that something comes to be drawn ListItem.data will contain that something which currently it doesnt always.

 

I was wondering if it could be because 10 models are being initialized with the same id and did research to find a way to have dynamicly created id but found nothing and as ive currently changed to just using images in scrollview and thats looking like the best outcome i'm cautious of changing the code back to listview without optimism and a way of creating dynamic ids to test the theory. 

 

So it's tricky to do and for a novice like myself it's going to even more difficult - If any further advice anyone can offer I would be grateful or any links to what I need to be doing.

 

Thanks.

Please use plain text.