【问题标题】:BlackBerry 10 QML Application Crashes on datasource LoadBlackBerry 10 QML 应用程序在数据源加载时崩溃
【发布时间】:2012-10-25 09:29:09
【问题描述】:

以下代码构造函数加载了一个 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();
        }
    }
}

【问题讨论】:

    标签: c++ qml blackberry-10 blackberry-cascades


    【解决方案1】:

    AbstractPane中有一个onCreationCompleted槽,你可以用它来调用load。

    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(); }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      相关资源
      最近更新 更多