以下代码构造函数加载一个Qml文件,该文件从远程Json数据源加载数据,这非常正常。当我调用startMenu方法时,我在MainMenu.Qml中使用完全相同的Qml,但是当我调用dataSource.load()时,我的应用程序在QDeclarativeExpression :: hasError()崩溃,之后它就会挂起。没有发出错误消息或异常。
如果我移动dataSource.load();单击按钮单击它可以工作,但是当页面加载时我无法加载我的数据。
#include "MyAppBB.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Button>
#include <bb/data/DataSource>
#include <wifi/wifi_service.h>
using namespace bb::cascades;
MyAppBB::MyAppBB(bb::cascades::Application *app) :
QObject(app) {
// create scene document from main.qml asset
// set parent to created document to ensure it exists for the whole application lifetime
bb::data::DataSource::registerQmlTypes();
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
this->myApp = app;
QDeclarativePropertyMap* propertyMap = new QDeclarativePropertyMap;
propertyMap->insert("name", QVariant(QString("Wes Barichak")));
propertyMap->insert("phone", QVariant(QString("519-555-0199")));
// create root object for the UI
this->root = qml->createRootObject<AbstractPane>();
qml->setContextProperty("propertyMap", propertyMap);
qml->setContextProperty("root", this);
QObject *newButton = root->findChild<QObject*>("btnLogin");
QObject::connect(newButton, SIGNAL(clicked()), this, SLOT(loginClick()));
// set created root object as a scene
app->setScene(root);
}
void MyAppBB::startMenu() {
QmlDocument *qml2 = QmlDocument::create("asset:///MainMenu.qml").parent(this);
AbstractPane *root2 = qml2->createRootObject<AbstractPane>();
this->myApp->setScene(root2);
}
这是Qml
import bb.cascades 1.0
import bb.data 1.0
Page {
Container {
layout: StackLayout {
}
TextField {
id: txtUsername
}
ListView {
id: myListView
// Associate the list view with the data model that's defined in the
// attachedObjects list
dataModel: dataModel
listItemComponents: [
ListItemComponent {
type: "item"
// Use a standard list item to display the data in the data
// model
StandardListItem {
title: ListItemData.identificationType
description: ListItemData.identificationTypeId
}
}
]
}
attachedObjects: [
GroupDataModel {
id: dataModel
// Sort the data in the data model by the "pubDate" field, in
// descending order, without any automatic grouping
sortingKeys: [
"ID"
]
sortedAscending: false
grouping: ItemGrouping.None
},
DataSource {
id: dataSource
source: "http://192.168.150.1/JsonMobileApiService/Service1.svc/GetIDTypes"
type: DataSourceType.Json
onDataLoaded: {
dataModel.clear();
dataModel.insertList(data);
}
}
]
onCreationCompleted: {
txtUsername.setText("boo!");
// Statement below causes crash
dataSource.load();
}
}
}
答案 0 :(得分:2)
onCreationCompleted
中有AbstractPane
个插槽,您可以使用它来调用负载。
以XMLDataAccess
的API规范为例。我正在使用它来自己加载XML文件中的数据,它可以正常工作
import bb.cascades 1.0
import bb.data 1.0
Page {
content: ListView {
id: listView
dataModel: dataModel
...
}
attachedObjects: [
GroupDataModel {
id: dataModel
},
DataSource {
id: dataSource
source: "contacts.xml"
query: "/contacts/contact"
onDataLoaded: {
dataModel.insertList(data);
}
}
]
onCreationCompleted: { dataSource.load(); }
}