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
New Contributor
ethridgt
Posts: 2
Registered: ‎07-05-2011
My Device: 9800

Why am I unable to load json from QML only?

I'm trying to use the example found under JsonDataAccess (https://developer.blackberry.com/cascades/reference/bb__data__jsondataaccess.html) to load a json file into a GroupDataModel using only QML.  It doesn't load and I'm not sure how determine why it is failing.  Can somebody provide some insight as to why this doesn't work for me?  Could you explain how to figure out and debug what is wrong when you are just using QML?  All I see is a white screen.

 

Here is my main.qml:

 

import bb.cascades 1.0

import bb.data 1.0

Page {

 

    content: ListView {

        id: listView

        dataModel: dataModel

        listItemComponents: [

            ListItemComponent {

                type: "item"

                Label {

                    text: ListItemData.title

                }

            }

        ]

    }

    attachedObjects: [

        GroupDataModel {

            id: dataModel

        },

        DataSource {

            id: dataSource

            source: "testdata.json"

            onDataLoaded: {

                dataModel.insertList(data)

            }

        }

    ]

    onCreationCompleted: {

        dataSource.load();

    }

}

 

Here is my testdata.json, which is located under the assets directory along with the main.qml file:

 

[

{"title":"Example 1"},

{"title":"Example 2"},

{"title":"Example 3"},

{"title":"Example 4"},

]

Developer
congdanhqx
Posts: 115
Registered: ‎10-22-2012
My Device: Developer

Re: Why am I unable to load json from QML only?

Did you registered DataSource to qml?

If you registered, may you provide the log (got by SSH to device and run the command "slog2info") so we can help you



Like my answer if it's helpful. Mark as answer if it help you solve the problem.
New Contributor
ethridgt
Posts: 2
Registered: ‎07-05-2011
My Device: 9800

Re: Why am I unable to load json from QML only?

I'm not sure what you mean by "Did you registered DataSource to qml?"  Can you explain in more detail?  The code you see above is from the example.  That is all that I did in my test of this functionality.

Developer
congdanhqx
Posts: 115
Registered: ‎10-22-2012
My Device: Developer

Re: Why am I unable to load json from QML only?

In the main function in main.cpp, you must call the function:

qmlRegisterType<bb::data&colon;:DataSource>();

 (You may call that function anywhere before create QmlDocument, too)



Like my answer if it's helpful. Mark as answer if it help you solve the problem.
Developer
congdanhqx
Posts: 115
Registered: ‎10-22-2012
My Device: Developer

Re: Why am I unable to load json from QML only?

The main.cpp should be this:

#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/data/DataSource>
#include <QLocale>
#include <QTranslator>

// include JS Debugger / CS Profiler enabler
// this feature is enabled by default in the debug build only
#include <Qt/qdeclarativedebug.h>

using namespace bb::cascades;

Q_DECL_EXPORT int main(int argc, char **argv)
{
    // this is where the server is started etc
    Application app(argc, argv);

    qmlRegisterType<DataSource>();
    /* or */
    // qmlRegisterType<DataSource>("bb.data", 1, 0, "DataSource");

    // localization support
    QTranslator translator;
    QString locale_string = QLocale().name();
    QString filename = QString( "<your_project_name>_%1" ).arg( locale_string );
    if (translator.load(filename, "app/native/qm")) {
        app.installTranslator( &translator );
    }

    // create the application pane object to init UI etc.
    new ApplicationUI(&app);

    // we complete the transaction started in the app constructor and start the client event loop here
    return Application::exec();
}

 



Like my answer if it's helpful. Mark as answer if it help you solve the problem.
Developer
congdanhqx
Posts: 115
Registered: ‎10-22-2012
My Device: Developer

Re: Why am I unable to load json from QML only?

[ Edited ]

Or you can call this function in C++

DataSource::registerQmlTypes();

 See this sample

https://github.com/blackberry/Cascades-Samples/tree/master/quotes

File

https://github.com/blackberry/Cascades-Samples/blob/master/quotes/src/quotesapp.cpp



Like my answer if it's helpful. Mark as answer if it help you solve the problem.
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: Why am I unable to load json from QML only?

is testdata.json in the same folder as your qml file?

If not try putting

source: "asset:///testdata.json"