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

Posts: 33
Registered: ‎05-02-2012
My Device: playbook
My Carrier: bouyig

GroupDataModel with a QObject subclass


I have create sa class town ( inherit from QObject) to insert in each GroupDatamodel, but when I display it in a listView, the list is empty. 

this is perhaps a problem with the inheritance from QObject

voici le code :


my class town.hpp that inherits from QObjet


#ifndef TOWN_HPP_
#define TOWN_HPP_
#include <QtCore/QLocale>
#include <QtCore/QObject>
#include <QtCore/QMetaType>

class town : public QObject {


Q_PROPERTY(QString m_sName READ getName WRITE setName  FINAL)
Q_PROPERTY(int m_iTemp READ getTemp WRITE setTemp FINAL)

    //town(QObject *parent = 0);

    town(QString  sName, int  iTemp);

QString getName() ;
int getTemp() ;

 Q_SLOT  void setName(QString newName);
  Q_SLOT  void setTemp(int newTemp);

    QString  m_sName;
    int m_iTemp;


#endif /* TOWN_HPP_ */






#include "town.hpp"

 town::town(QString sName, int iTemp)
	m_sName = sName;
	m_iTemp = iTemp;

QString town::getName() { return m_sName;}
int town::getTemp() { return m_iTemp;}

void town::setName(QString sName) { m_sName = sName;}
void town::setTemp(int iTemp) { m_iTemp = iTemp;}

 my app.cpp

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/GroupDataModel>
//#include <bb/cascades/DataModel>

#include "app.hpp"

#include "town.hpp"

using namespace bb::cascades;



    QmlDocument *qml = QmlDocument::create("main.qml");
    qml->setContextProperty("cs", this);
    AbstractPane *root = qml->createRootNode<AbstractPane>();
GroupDataModel *pModel = root->findChild<GroupDataModel*>("groupDataModel");

    pModel->insert(new town("paris",75));
    pModel->insert(new town("Lamber", 479));
    pModel->insert(new town("zurich", 696));
    pModel->insert(new town("marseille",269));
    pModel->insert(new town("basel",712));
    town *testdeses=new town("london",75);





import bb.cascades 1.0

Page {
    content: Container {
        background: Color.create("#272727");
        attachedObjects: [
            GroupDataModel {
                id: groupDataModel
                objectName: "groupDataModel"
        ListView {
            id: listView
            dataModel: groupDataModel

        } // end of ListView
    } // end of Container
} // 


Posts: 60
Registered: ‎05-10-2012
My Device: Blackberry 10 Alpha
My Carrier: none

Re: GroupDataModel with a QObject subclass



instead of


GroupDataModel *pModel = root->findChild<GroupDataModel*>("groupDataModel");


I would try to get the ListView itself and retrieve the attached DataModel, rather than querying for the DataModel by name (you can so create a new GroupDataModel in c++ and attach it to the list view).


 It is also possible you need to wait for the QML to get constructed by waiting for the onCreationCompleted event before using findChild to retrieve the objectName.


And for robustness, do null pointer checking before using pointers from findChild() calls