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
Developer
Posts: 120
Registered: ‎02-06-2013
My Device: DAB, DAC, Lost red Z10, Regular Replaced Z10, PlayBook
My Carrier: airtel

Loading in main.qml

QVariantMap firstEmployee, secondEmployee;

    firstEmployee["firstName"] = QVariant("Mike");
    firstEmployee["lastName"] = QVariant("Chepesky");
    firstEmployee["number"] = QVariant("51649875");
    secondEmployee["firstName"] = QVariant("Westlee");
    secondEmployee["lastName"] = QVariant("Barichak");
    secondEmployee["number"] = QVariant("63257801");

    QVariantList myEmployeeList = QVariantList() << QVariant(firstEmployee)
                               << QVariant(secondEmployee);

    QVariantMap topLevelEmployeeMap;
    topLevelEmployeeMap[".root"] = QVariant("employees");
    topLevelEmployeeMap["employee"] = QVariant(myEmployeeList);
    QVariant myData = QVariant(topLevelEmployeeMap);

    QFile file(QDir::temp().absoluteFilePath("myXMLFile.xml"));

    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {

        XmlDataAccess xda;
        xda.save(myData, &file);
    }
    file.close();
    Page *root = new Page;
    ListView *listView = new ListView;

    GroupDataModel *model = new GroupDataModel(QStringList() << "firstName"
                                               << "lastName");

    XmlDataAccess xda;
    QDir temp = QDir::temp();
    QVariant list = xda.load(temp.absoluteFilePath("myXMLFile.xml"),
                             "/employees/employee");
    model->insertList(list.value<QVariantList>());

    listView->setDataModel(model);

    root->setContent(listView);

 Though this works fine, I would like to load the list in the listview from my main.qml, not sure how to go about it



Looking for SMTP Servers, Bulk Emails and Bulk SMS services at an affordable price to help boost your business? Look no further. I've the solution for your needs. Contact me now.
Highlighted
Developer
Posts: 161
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: Loading in main.qml

Maybe this:

 

attachedObjects: [
  DataSource {
    id: dataSource
    source: your_xmlfile_path
    onDataLoaded: {
      your_data_model.insert(data);
    }
  }
]

onCreationCompleted: {
  dataSource.load();
}

 

Regards.

If my answer has been useful to you, please, click the button
Developer
Posts: 120
Registered: ‎02-06-2013
My Device: DAB, DAC, Lost red Z10, Regular Replaced Z10, PlayBook
My Carrier: airtel

Re: Loading in main.qml

well i think you need to locate the xml file from QML first

 

main.cpp

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

    QString workingDir = QDir::currentPath();
    
    QDeclarativePropertyMap* location = new QDeclarativePropertyMap;
    location->insert("myxml", QVariant(QString("file://"+workingDir+"/shared/voice/")));
    qml->setContextProperty("location", location);

 

main.qml

Page {
    Container {
        ListView {
            dataModel: XmlDataModel {
                source: location.myxml + "myXMLFile.xml"
            }
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    
                    StandardListItem {
                        title: ListItemData.firstName
                    }
                }
            ]
        }
    }
}

 but in the console, it says: XmlDataModel could not open the file  "file:///accounts/1000/appdata ......."

any thoughts ??



Looking for SMTP Servers, Bulk Emails and Bulk SMS services at an affordable price to help boost your business? Look no further. I've the solution for your needs. Contact me now.
Developer
Posts: 161
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: Loading in main.qml

Have you set the access_shared permission?

Regards.

If my answer has been useful to you, please, click the button
Developer
Posts: 120
Registered: ‎02-06-2013
My Device: DAB, DAC, Lost red Z10, Regular Replaced Z10, PlayBook
My Carrier: airtel

Re: Loading in main.qml

Yes, It's already set.

 

Here I've the updated code:

 

app.cpp

   
QVariantMap firstEmployee, secondEmployee; firstEmployee["firstName"] = QVariant("Mike"); firstEmployee["lastName"] = QVariant("Chepesky"); firstEmployee["number"] = QVariant("51649875"); secondEmployee["firstName"] = QVariant("Westlee"); secondEmployee["lastName"] = QVariant("Barichak"); secondEmployee["number"] = QVariant("63257801"); QVariantList myEmployeeList = QVariantList() << QVariant(firstEmployee) << QVariant(secondEmployee); QVariantMap topLevelEmployeeMap; topLevelEmployeeMap[".root"] = QVariant("employees"); topLevelEmployeeMap["employee"] = QVariant(myEmployeeList); QVariant myData = QVariant(topLevelEmployeeMap); QString path = QDir::currentPath(); QFile file(path+"/data/names.xml"); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { XmlDataAccess xda; xda.save(myData, &file); } file.close();

 main.cpp

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(&app);
    
    QString workingDir = QDir::currentPath();

    QDeclarativePropertyMap* dirPaths = new QDeclarativePropertyMap;
    dirPaths->insert("myxml", QVariant(QString("file://"+workingDir+"/app/native/assets/")));
    qml->setContextProperty("dirPaths", dirPaths);

 main.qml

Page {
    Container {
        ListView {
            dataModel: XmlDataModel {
                source: dirPaths.myxml + "names.xml"
            }
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    
                    StandardListItem {
                        title: ListItemData.firstName
                    }
                }
            ]
        }
    }
} 

 this time no error but doesn't seem to display anything in the list



Looking for SMTP Servers, Bulk Emails and Bulk SMS services at an affordable price to help boost your business? Look no further. I've the solution for your needs. Contact me now.
Developer
Posts: 161
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: Loading in main.qml

[ Edited ]

Don't forget the type of the ListItemComponent must match the xml tags.

Regards.

If my answer has been useful to you, please, click the button