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
candll
Posts: 19
Registered: ‎05-09-2013
My Device: Z10
My Carrier: None
Accepted Solution

very simple custom DataModel derived from bb::cascades::DataModel won't display data in ListView

[ Edited ]

Hi,

 

A custom data model I made in C++ doesn't display anything in a ListView

It seems that the binding

 

listView.dataModel = _dataModel

 

on QML side won't ask the DataModel for data. I am not really sure what I am doing wrong.

 

PS. Placing the CustomDataModel in QML as an attachedObject and inserting data there works, but I have a specific need to keep the DataModel in C++.

 

Below example of code reduced to the simplest case that Is causing me trouble:

 

//customdatamodel.h

#include <QObject>
#include <QDebug>
#include <bb/cascades/DataModel>

class CustomDataModel : public bb::cascades::DataModel
{
Q_OBJECT
  public:
    CustomDataModel(QObject * parent=0) : bb::cascades::DataModel(parent)
    {
    }
    virtual void ~CustomDataModel() 
	{
	} 

	virtual int CustomDataModel::childCount(const QVariantList& indexPath)
	{   //just one level deep
		return 0;
	}

	virtual bool CustomDataModel::hasChildren(const QVariantList& indexPath) 
	{
		return childCount(indexPath) > 0;
	}

	virtual QVariant CustomDataModel::data(const QVariantList& indexPath)
	{ 
		if (indexPath.count() == 1)
		{
			return m_values[indexPath[0].toInt()];
		} 
		return QVariant();
	}


    void CustomDataModel::insert(const QVariantMap &item)
	{
		 m_values.append(item);
		 emit itemAdded(QVariantList() << m_values.count()-1);
	}
	
	private:
	  QVariantList m_values;
}


//application.cpp

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
: QObject(app)
{ 
    qmlRegisterType<CustomDataModel>("app.datamodels", 1,0, "CustomDataModel"); 
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
 
    CustomDataModel *dataModel = new CustomDataModel(this);
	
	QVariantMap map;
	map["field"] = "value1";
	dataModel->insert(map);
	
	map = QVariantMap();
        map["field"] = "value2";
	dataModel->insert(map);
	
	map = QVariantMap();
	map["field"] = "value3";
	dataModel->insert(map);
	
	qml->setContextProperty("_dataModel", dataModel);
	
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);
}


//main.qml

    Page {  
        Container {
            layout: DockLayout {
            }

            ListView { 
                id:listView
                dataModel: _dataModel
				
				function itemType(data, indexPath) 
				{
					if (indexPath.length == 1)
					{
						return "item";
					} 
				}
				
                listItemComponents: [

                    ListItemComponent {
                        type: "item"
                        StandardListItem { 
                            title: ListItemData.field
                        }
                    }
				]
			}
		}
	}

 

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

Re: very simple custom DataModel derived from bb::cascades::DataModel won't display data in ListView

Hi,
You're always returning a zero childCount, so it thinks that the model is empty.

Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
candll
Posts: 19
Registered: ‎05-09-2013
My Device: Z10
My Carrier: None

Re: very simple custom DataModel derived from bb::cascades::DataModel won't display data in ListView

[ Edited ]

Ah Thank you. I see the mistake. I thought childCount is supposed to return 0 for header level items (i.e where indexPath.size() == 1), but in reality it should return their total count.for each indexPath.size()

Please use plain text.