[ create a new paste ] login | about

Link: http://codepad.org/u7IsmiWT    [ raw code | fork ]

C++, pasted on Jun 11:
// view.qml

import bb.cascades 1.0

Page {
    content: Container {
        ListView {
            dataModel: _app.model
            
            // The ListView uses this to determine the
            // type of each ListItem. Since we use a
            // QMapListDataModel we return the 'type' entry.
            function itemType (data, indexPath) {
                return data["type"];
            }
            listItemComponents: [

                // customize how to display the data

            ]
        }
    }
}


// App.hpp

#ifndef APP_HPP
#define APP_HPP

#include <QtCore/QObject>
#include <QtNetwork/QNetworkAccessManager>

#include <bb/cascades/QListDataModel>

class App: public QObject
{
    Q_OBJECT

    // This property makes the posts model available to the UI
    Q_PROPERTY(bb::cascades::DataModel *model READ model NOTIFY modelChanged)

public:
    App();

    /**
     * The corresponding read function to the model property.
     */
    bb::cascades::DataModel * model() const;

Q_SIGNALS:
    /**
     * The corresponding notify signal to the property.
     */
    void modelChanged();

private Q_SLOTS:
    /**
     * Called when a network query provides new data.
     *
     * @param reply The reply details.
     */
    void handleNetworkData(QNetworkReply *reply);

private:
    /**
     * The corresponding data model to the posts property.
     */
    mutable bb::cascades::QMapListDataModel *dataModel;

    /**
     * The network manager that handles the communication with the web service.
     */
    QNetworkAccessManager networkManager;

};

#endif // APP_HPP


// App.cpp

#include <bb/cascades/Page>
#include <bb/cascades/QmlDocument>
#include <bb/data/JsonDataAccess>

#include "App.hpp"

using namespace bb::cascades;
using namespace bb::data;

App::App() :
{

    // Load up the QML for our view
    QmlDocument *qml = QmlDocument::create("view.qml");

    // Provide a reference to this class
    qml->setContextProperty("_app", this);

    // Create the root node of this view
    Page *view = qml->createRootNode<Page>();

    dataModel = new QMapListDataModel();

    // Hook this signal so we can respond to network replies
    connect(&networkManager, SIGNAL(finished(QNetworkReply *)), this,
            SLOT(handleNetworkData(QNetworkReply *)));

    // Do the request
    QUrl url = "http://someservice.com/v1/do/blah";
    networkManager.get(QNetworkRequest(url));

}

DataModel * App::model() const
{
    return dataModel;
}

void App::handleNetworkData(QNetworkReply *reply)
{

    if (!reply->error()) {
    
        // Let's get ALL the data
        const QByteArray response(reply->readAll());

        // and parse the JSON into a usable format
        JsonDataAccess jda;
        QVariantMap results = jda.loadFromBuffer(response).toMap();

        // Get the relevant parts we want from the JSON
        QVariantList posts = results["data"].toMap()["children"].toList();

        Q_FOREACH(QVariant post, posts) {

            // Perhaps do something with the data

            ....


            // and then add it to the model
            dataModel->append(post);

        }
    
    }

    // Cleanup
    reply->deleteLater();

}


Create a new paste based on this one


Comments: