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
Trusted Contributor
Brennan12325
Posts: 204
Registered: ‎05-15-2012
My Device: None
My Carrier: Telus
Accepted Solution

Displaying ListItemData from QList<QVariantMap> with QListDataModel<QVariantMap>

Hey,

 

I have a List declared like so:

ListView {
                id: PostsList
                preferredWidth: 700
                objectName: "PostsList"
                layoutProperties: DockLayoutProperties {
                    horizontalAlignment: HorizontalAlignment.Center
                }
                            
                // A single component because there's no list header.
                listItemComponents: [
                    ListItemComponent {
                        type: "post"
                        PostItem {
                        }
                    }
                ]
                
//Tried this hoping items were being misparsed as headers. function itemType(data, indexPath) { return 'item'; } }

 

Where PostItem.qml is

Container {
    id: itemRoot
    layout: DockLayout {
        leftPadding: 30
        rightPadding: leftPadding
        topPadding: 30
    }
    
    Container {
        preferredHeight: 250
        layout: DockLayout {
        }
        Label {
		    //text: ListItemData.data.title
		    text: ListItemData.title
		    layoutProperties: DockLayoutProperties {
	            verticalAlignment: VerticalAlignment.Center
	            horizontalAlignment: HorizontalAlignment.Center
             }
        }
    Label {
            //text: ListItemData.data.title
            text: ListItemData.description
            layoutProperties: DockLayoutProperties {
                verticalAlignment: VerticalAlignment.Center
                horizontalAlignment: HorizontalAlignment.Center
             }
        }
 } // Signal handler for list item activation. ListItem.onActiveChanged: { setHighlight (ListItem.active); } // Signal handler for list item selection. ListItem.onSelectedChanged: { setHighlight (ListItem.selected); } }

 If you're starting to feel like this code looks like stamp collector you're on to something....

 

Anyways, I'm declaring the data model for the list as follows, this code is running in a QNetworkAccessManager callback SLOT function...

QList<QVariantMap> map;
QVariantMap variant;
foreach (post, posts) { variant.clear(); variant.insert("id", post->id); variant.insert("title", post->title); variant.insert("author", post->author); map.insert(map.length(), variant); } ListView *PostsList = mNav->findChild<ListView*>("PostsList"); QListDataModel<QVariantMap> *model = new QListDataModel<QVariantMap>; model->setParent(this); model->clear(); QVariantMap v; int i = 0; foreach(v, map) { model->insert(i,v); i++; } PostsList->setDataModel(model);

 

So here's what I expect to happen.

 

I compile and debug the program. Some code I have sends the network request. I parse the JSON into a QList<Post> (which is my own type). From their I parse it into a QList<QVariantMap> (called map in the above code). Once I set my ListView Data Model as the variable model as per this line:

 

PostsList->setDataModel(model);

 

The list "PostsList" refreshes in the app and displays the data from post->title, as inserted into the QVariant that was inserted into QVariantMap on this line:

 

variant.insert("title", post->title);

 

Instead, what is displayed is the post->author data, and the only reason I can possible conceive for that to be displayed (in reality the item post has about 13 members, I've removed most for length) is that it is alphabetically first and appears to be the first member of the QVariantMap variable. The post author is the single label shown, so the two declared labels in the QML appear to be ignored.

 

When I select a list item (using code basically identical to the stampcollector sample) and the page is displayed via the QDeclarativeContext method all the members of the QVariantMap are accessible and can be used to display QML elements.

 

Here's a sample of how the stamp collector app accomplishes that in the OnSelectionChanged handler:

 

ListView* PostsList = dynamic_cast<ListView*>(sender());
DataModel* Model = PostsList->dataModel();

// Update the content view context property so that it corresponds to
// the selected item and navigate to the page.
QVariantMap map = Model->data(indexPath).toMap();
mQmlContext->setContextProperty("_contentView", map);
mNav->push(mContentPage);

 

Can anyone explain the behavior I'm getting? Why can't I access items via the ListItemData.member method described in the documentation?

 

Note: some minor code changes have been made for length and clarity, the code has no compiler errors, and like I said the DataModel code is working at least to some extent as all the data members I expect to be accessible in ListItemData are accessible in _contentView.

 

 

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!
Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Displaying ListItemData from QList<QVariantMap> with QListDataModel<QVariantMap>

I haven't checked in depth, but the first thing I would try is change your function to return "post" instead of "item", since you have a component for "post".   I'll try an example.

 

Stuart

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Displaying ListItemData from QList<QVariantMap> with QListDataModel<QVariantMap>

See sample weatherguesser, weatherpage.qml, "todayItem"

 

Stuart

Please use plain text.
Trusted Contributor
Brennan12325
Posts: 204
Registered: ‎05-15-2012
My Device: None
My Carrier: Telus

Re: Displaying ListItemData from QList<QVariantMap> with QListDataModel<QVariantMap>

[ Edited ]

Depressingly the only issue was returning "item" instead of "post"  -_-

 

Good thing I typed out that 14 page question.

 

edit: Perhaps I shouldn't be depressed. I've had my fair share of issues that involved much more complex and infuriating solutions.

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!
Please use plain text.
Contributor
girish_bb10
Posts: 12
Registered: ‎12-15-2012
My Device: BB 10 Alpha
My Carrier: Blackberry

Displaying ListItemData from QList<CustomClass> with QListDataModel<CustomClass>;

[ Edited ]

I have My custom Class. I am setting values for that. I want to show the data from the QListDataModel to list view. Can any body tel me how to set the values to text property of listviewcomponent. ?

if

 

QListDataModel<QVariantMap> then we do 

 

 listItemComponents: [

                ListItemComponent {

                    type: "item"

                    

                    Label {

                        id : mNameLabel

                      

                        text: ListItemData.title (here title is key)

                        preferredWidth: 500

                        topPadding: 20  

                        horizontalAlignment: HorizontalAlignment.Left

                                      

                        textStyle {

                            base: SystemDefaults.TextStyles.TitleText

                            fontWeight: FontWeight.Normal

                        }

                        onCreationCompleted: {

                            _logger.log("onCreationCompletedofLABLE"+ListItemData);

                            }

                          

                    }//label 

                }//ListItemComponent

 

 

How to set the text when i am using my class in QListDataModel<MyClass> ?

Please use plain text.
Trusted Contributor
Brennan12325
Posts: 204
Registered: ‎05-15-2012
My Device: None
My Carrier: Telus

Re: Displaying ListItemData from QList<CustomClass> with QListDataModel<CustomClass>;

You appear to be accessing your DataModel data correctly, "ListItemData.title" should work.

 

When you say QListDataModel<MyClass>, though, you should note that whatever 'your class' is will need to be a QVariant (or perhaps an extension of QVariant, I have only used QListDataModel with QVariant so I'm not sure).

 

To set the DataModel in C++ you would use this code:

ListView *List = yourQmlPage->findChild<ListView*>("yourList");
List ->setDataModel(&YOUR_QLISTDATAMODEL);

 where "yourList" is an id value you've assigned your ListView in QML.

 

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!
Please use plain text.
Contributor
girish_bb10
Posts: 12
Registered: ‎12-15-2012
My Device: BB 10 Alpha
My Carrier: Blackberry

Re: Displaying ListItemData from QList<CustomClass> with QListDataModel<CustomClass>;

Thnaks!!

 

I am using same way but it is not displaying anything in the list view

Please use plain text.