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
New Contributor
ujwalapatil
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::smileyfrustrated:ampleQlistDataModel(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);

 

}

Please use plain text.
Developer
rashm2k
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.

Please use plain text.
Developer
rashm2k
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.

Please use plain text.