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.

Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

How to create a listview using xmldatamodel in cpp ....?



I want to create a listview using XmlDataModel in cpp.I know how to create a listview using xmldatamodel in qml but i am unable to create a listview using xmldatamodel in cpp.Please provide me the code.....




Naresh Kodumuri.

Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to create a listview using xmldatamodel in cpp ....?

Look at cascadescookbookcpp, sheetrecipe



Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE
My Carrier: AirTel

Re: How to create a listview using xmldatamodel in cpp ....?

[ Edited ]

Hi ,

If you want simple application then this example helpful for you otherwise go with BlackBerry Development Advisor suggetion.





<?xml version="1.0" encoding="UTF-8" ?>
    <person id="1">
    <person id="2">
    <person id="3">



Cpp Code


#include <QObject>
#include <QIODevice>

//#include <QXmlStreamReader>
#include <QtXml/QDomDocument>
#include <bb/cascades/QListDataModel>

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

using namespace bb::cascades;

App::App() {
    // Load the QML document and retrieve the root node
    QmlDocument *qml = QmlDocument::create("main.qml");
    AbstractPane *root = qml->createRootNode<AbstractPane>();

    GroupDataModel *model = new GroupDataModel(
            QStringList() << "firstname" << "surname" << "email");

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

    QString appFolder(QDir::currentPath());
    QString fileName = appFolder + "/app/native/assets/models/items.xml";
    fprintf(stderr, "File Path :  %s\n", fileName.toAscii().constData());

    QFile file(fileName);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        fprintf(stderr, "Error to open file.\n");
    } else {
        fprintf(stderr, "File opened.\n");

    QList<QMap<QString, QString> > persons;

    QDomDocument doc("mydocument");
    if (!doc.setContent(&file)) {

    //Get the root element
    QDomElement docElem = doc.documentElement();

    // you could check the root tag name here if it matters
    QString rootTag = docElem.tagName(); // == persons

    // get the node's interested in, this time only caring about person's
    QDomNodeList nodeList = docElem.elementsByTagName("person");

    //Check each node one by one.
    QMap<QString, QVariant> person;
    for (int ii = 0; ii < nodeList.count(); ii++) {

        // get the current one as QDomElement
        QDomElement el = nodeList.at(ii).toElement();

        //    person["id"] = el.attribute("id"); // get and set the attribute ID

        //get all data for the element, by looping through all child elements
        QDomNode pEntries = el.firstChild();
        while (!pEntries.isNull()) {
            QDomElement peData = pEntries.toElement();
            QString tagNam = peData.tagName();

            if (tagNam == "firstname") {
                //We've found first name.
                person["firstname"] = peData.text();
            } else if (tagNam == "surname") {
                //We've found surname.
                person["surname"] = peData.text();
            } else if (tagNam == "email") {
                //We've found email.
                person["email"] = peData.text();
            } else if (tagNam == "website") {
                //We've found website.
                person["website"] = peData.text();
            pEntries = pEntries.nextSibling();


    // Set the scene using the root node



Qml File


import bb.cascades 1.0

Page {
    content: Container {
        ListView {
            id: contactList
            objectName: "listView"
            listItemComponents: [
                ListItemComponent {
                    type: "header"
                    HeaderListItem {
                        title: ListItemData
                ListItemComponent {
                    type: "item"
                    Container {
                        Label {
                            text: ListItemData.firstname
                        Label {
                            text: ListItemData.surname
                        Label {
                            text: ListItemData.email
                        Container {
                            preferredWidth: 768
                            preferredHeight: 5
                            background: Color.Red
            ] // end of listItemComponents list        



Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

Re: How to create a listview using xmldatamodel in cpp ....?



Thanks for ur reply.Now i got an idea.



Naresh Kodumuri.