ListView *listView = root->findChild<ListView*>("listView");
показывает, что корень не был отклонен в областиМой код здесь
1. ФАЙЛ QML
import bb.cascades 1.0
import bb.data 1.0
Page {
content: Container {
layout: StackLayout {
orientation: LayoutOrientation.TopToBottom
}
TextField {
id: countryID
hintText: "Enter Country ID eg:'1'"maxWidth: 400
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
textStyle.textAlign: TextAlign.Center
}Button {
id: btn
text: "Send JSON Request"onClicked: {
app.sendRequest(countryID.text);
}
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
}
ListView {
objectName: "listView"listItemComponents: [
ListItemComponent {
StandardListItem {
id: item
title: ListItemData.CategoryName
description: ListItemData.CategoryID
}
}
]
}} //end of container
} //end of page
2. Файл ГЭС
// Default empty project template
#ifndef CALCI_HPP_
#define CALCI_HPP_
#include <QObject>
#include <bb/cascades/QListDataModel>namespace bb { namespace cascades { class Application; class ListView; }}
class controller : public QObject
{
Q_OBJECT
public:
controller(bb::cascades::Application *app);
public Q_SLOTS:
void sendRequest(const QString &countryID);
private Q_SLOTS:
void onFinished();
};#endif
3. ФАЙЛ CPP
// Default empty project template
#include "calci.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/ListView>
#include <bb/cascades/ArrayDataModel>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QUrl>
#include <bb/data/JsonDataAccess>
using namespace bb::cascades;
using namespace bb::data;
controller::controller(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);
}void controller::sendRequest(const QString &countryID)
{
QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);
const QString queryUri = QString::fromLatin1("http://192.168.1.251:410/Mobile/Service1.svc/english/Category?CountryID=%1").arg(countryID);
QNetworkRequest request(queryUri);
QNetworkReply* reply = networkAccessManager->get(request);
bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
Q_ASSERT(ok);
Q_UNUSED(ok);
}
void controller::onFinished()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
ArrayDataModel *model = new ArrayDataModel();
QString response;
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200)
{
JsonDataAccess jda;
QVariantMap map = jda.loadFromBuffer(reply->readAll()).toMap();
QVariantList addresses = map["GetCategoryResult"].toList();
foreach(QVariant var, addresses) {
QVariantMap addressMap = var.toMap();
qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
qDebug() << "CategoryID is " << addressMap["ThumnailImage"].toUrl();
model->append(addressMap);
}
ListView *listView = root->findChild<ListView*>("listView");
listView->setDataModel(model);
}
else {
qDebug() << "Server returned code " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}
}
QUERY:
Представление списка не может быть в состоянии создать
qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
qDebug() << "CategoryID is " << addressMap["ThumnailImage"].toUrl();
model->append(addressMap);
}
ListView *listView = root->findChild<ListView*>("listView");
listView->setDataModel(model);
Вы должны создать свой ListView в QML и ссылаться на него из cpp. Затем вы можете добавить всю адресную карту к вашей ArrayDataModel, а затем ссылаться на ключи по мере необходимости в компоненте элемента.
CPP:
foreach(QVariant var, addresses) {
QVariantMap addressMap = var.toMap();
qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
qDebug() << "CategoryID is " << addressMap["ThumnailImage"].toUrl();
model->append(addressMap);
}
ListView *listView = root->findChild<ListView*>("listView");`
listView->setDataModel(model);
QML:
ListView {
objectName: "listView"listItemComponents: [
ListItemComponent {
StandardListItem {
id: item
title: ListItemData.CategoryName
imageSource: "asset:///images/" + ListItemData.ThumnailImage // if the image is local
}
}
]
}
Других решений пока нет …