如何在 Blackberry Cascades 中创建用于 JSON 解析的列表视图
how to create listview for json parsing in blackberry cascades
- 我创建了 json 网络服务。
- 我在 cpp 文件中得到了响应
- 如何在 qml 页面中显示收到的 json 数据
-
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. HPP 文件
// 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();
}
}
查询:
列表视图无法创建
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,并从 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
}
}
]
}
相关文章:
- 在c++中使用nlohmann从类到json的转换
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 视图中的参数推导失败:take_while
- C++json插入数组
- nlohmann-json将一个数组插入到另一个数组中
- 如何使用curlpp通过POST方法上传文件和json数据
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在 nlohmann 的 json 库中获取数组长度?
- 在另一个类视图中添加最多2个图表的正确方法是什么
- UE4 C++ 我无法将 JSON 嵌套值获取到 TArray
- 如何在特定位置追加 json 文件
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- OpenVR:向视图方向移动
- 使用 WIN32 API (C/C++) 对特定树视图项进行着色
- 将所选值(通过视图)从 boost::multi_array 复制到另一个数组 (C++)
- 使用 jsoncpp 解析 json 数组字符串
- 如何在 Blackberry Cascades 中创建用于 JSON 解析的列表视图