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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
New Contributor
Posts: 3
Registered: ‎12-28-2012
My Device: BB10 Dev Alpha
My Carrier: Blackberry

Populate data into Listview using QListDataModel

Hi,

 

I want to use QListDataModel with my custom object named Filter, and show "name" (Q_PROPERTY of Filter.h) in the listview. 

 

issue is Once I set my ListView Data Model as the variable mode, i am not getting any info in listview.

i am accessing name property in main.qml like label:{ text : Listitemdata.name} . But listview is blank.

 

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

 

 

 

 


 

 1.main.qml 

 

Page {

    Container {

        layout: DockLayout {}

        ListView {

            id : myListView

            objectName: "myListView"

            verticalAlignment: VerticalAlignment.Center

            

            listItemComponents: [

                ListItemComponent {

                    type: "item"

                    

                    Container {

                        id : highlightContainer

                        preferredHeight: 200

                        layout: AbsoluteLayout {  }

                      

                         

                        Label {

                            id : mNameLabel

                            text: ListItemData.name  

                            preferredWidth: 500

                            topPadding: 20  

                            horizontalAlignment: HorizontalAlignment.Left

                           

                            textStyle {

                                base: SystemDefaults.TextStyles.TitleText

                                fontWeight: FontWeight.Normal

                            }

                        }//label

                    }//inner container 

                

                }//ListItemComponent

            ]//listItemComponents

            

           

        }//ListView

    

    }//Container

}//Page

 


2. Filter.h

 

#ifndef FILTER_H_

#define FILTER_H_

 

#include<QObject>

#include<qmetatype.h>

 

classFilter : publicQObject {

Q_OBJECT

 

Q_PROPERTY ( QString name READ name WRITE setName NOTIFY nameChanged)

Q_PROPERTY ( QString imageUrl READ imageUrl WRITE setImageUrl NOTIFY imageUrlChanged)

 

public:

 

 

Filter();

 

virtual ~Filter();

 

 public Q_SLOTS:

 void setName(QString _name);

 void setImageUrl(QString _imageUrl);

 QString name();

 QString imageUrl();

 

 signals:

 void nameChanged();

 void imageUrlChanged();

 

private:

QStringmName;

QStringmImageUrl;

 

};

 

Q_DECLARE_METATYPE(Filter *);

 

 

#endif/* FILTER_H_ */

 


3. Filter.cpp

 

Filter::Filter()

{

// TODO Auto-generated constructor stub

 

}

 

Filter::~Filter()

{

// TODO Auto-generated destructor stub

}

 

void Filter::setName(QString _name)

{

mName = _name;

}

 

QString Filter::name()

{

    return mName;

}

void Filter::setImageUrl(QString _imageUrl){

mImageUrl = _imageUrl;

}

 

QString Filter::imageUrl(){

returnmImageUrl;

}

 

 

 


4. SampleQlistDataModel.cpp

 

 

SampleQlistDataModel:Smiley FrustratedampleQlistDataModel(bb::cascades::Application *app)

: QObject(app)

{

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

qml->setContextProperty("_app", this);

 

AbstractPane *root = qml->createRootObject<AbstractPane>();

 

app->setScene(root);

 

ListView* myList = root->findChild<ListView*>("myListView");

 

 

QListDataModel<Filter*> *model = newQListDataModel<Filter*>;

 

model->setParent(this);

 

model->clear();

 

for (int i  = 0; i  < 10; ++ i ) {

Filter *filetrModel = new Filter;

 

filetrModel->setName("name " + QString::number(i));

filetrModel->setImageUrl("image" + QString::number(i));

model->insert(i,filetrModel);

}

 

 

myList->setDataModel(model);

myList->setVisible(true);

 

}

Developer
Posts: 18
Registered: ‎10-12-2012
My Device: Playbook
My Carrier: none

Re: Populate data into Listview using QListDataModel

I've been trying to do the same thing, see this example for how to do it:

 

http://blackberry.github.com/Qt2Cascades-Samples/docs/qftp.html

 

I haven't been able to get it to work yet, but working on it.

Highlighted
Developer
Posts: 18
Registered: ‎10-12-2012
My Device: Playbook
My Carrier: none

Re: Populate data into Listview using QListDataModel

I have got it working. In order to make this work properly, you need to do two things,

 

at the end of your .h after }; you need to add Q_DECLARE_METATYPE(YourClass*);

 

THEN 

 

also do qmlRegisterType<YourClass>("com.example", 1,0, "YourClass");

 

This then works fine.