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
Trusted Contributor
AllSystemGo
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers
Accepted Solution

How to send data from c++ to qml to another qml.

Hi,

 

   I'm quite confused on a simple problem I'm sure. Let me post some of my code and then I'll ask my question

 

main.qml

 

// Default empty project template
import bb.cascades 1.0
import bb.data 1.0

// creates one page with a label

TabbedPane {
    Menu.definition: MenuDefinition {
        actions: [
            ActionItem {
                title: "Refresh"
            }
        ]
    }
    showTabsOnActionBar: true
    Tab {
        title: qsTr("Employee")
        NavigationPane {
            id: everyonePane
            Page {
                id: everyoneFeed
                ListView {
                    objectName: "FeedView"
                    id: FeedView

                    layout: StackListLayout {
                        headerMode: ListHeaderMode.Sticky
                    }
                    listItemComponents: [
                        ListItemComponent {
                            type: "item"
                            DetailFeed {
                            }
                        }
                    ]
                }
            }
            attachedObjects: [
                ActivityIndicator {
                    objectName: "indicator"
                    verticalAlignment: VerticalAlignment.Center
                    horizontalAlignment: HorizontalAlignment.Center
                    preferredHeight: 200
                    preferredWidth: 200
                }
            ]
            onCreationCompleted: {
                
            }
        }
    }
    Tab {
        title: qsTr("TimeSheet")
        Page {
        }
    }
    Tab {
        title: qsTr("Calendar")
        Page {
        }
    }
}

 

detailView

 

import bb.cascades 1.0

Container {
    layout: StackLayout {
        orientation: LayoutOrientation.TopToBottom // this line stacks everything below
    }
    bottomPadding: 20
    Container { // single row
        id: row1
        // individual row container
        layout: DockLayout {
        }
        preferredWidth: maxWidth
        Container { // container for image
            preferredWidth: 150 //size of image if known
            preferredHeight: 150
            topPadding: 10
            leftPadding: 20
            horizontalAlignment: HorizontalAlignment.Left
            verticalAlignment: VerticalAlignment.Center
            ImageView {
                preferredWidth: 150 //size of image if known
                preferredHeight: 150
                imageSource: "asset:///images/person.jpg" // some image
            }
        }
        // stack labels
        Container { // container for labels
            horizontalAlignment: HorizontalAlignment.Left // align this container to the left
            translationX: 200
            verticalAlignment: VerticalAlignment.Center
            layout: StackLayout {
                orientation: LayoutOrientation.TopToBottom // this stacks the labels
            }
            Label {
objectName: "firstname" text: "first label" } Label { text: "second label" } } } Divider { } }

 

employee.cpp

// Default empty project template
#include "Employee.hpp"

#include <bb/cascades/Application>
#include <bb/data/DataSource>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>

#include <bb/cascades/GroupDataModel>
#include <bb/data/JsonDataAccess>

using namespace bb::cascades;

QString mQueryUri;

Employee::Employee(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
	mQml = QmlDocument::create("asset:///main.qml").parent(this);

	// create root object for the UI
	mRoot = mQml->createRootObject<AbstractPane>();

	// Retrieving my activity indicator
	//mActivityIndicator = mRoot->findChild<ActivityIndicator*>("indicator");
	//mActivityIndicator->start();

	// Retrieving my list from QML
	mListView = mRoot->findChild<ListView*>("dribbbleFeedView");
	GetFeed("everyone");

	// set created root object as a scene
	app->setScene(mRoot);
}

void Employee::GetFeed(QString feedType) {
	// First off we initialize our NetworkManager
	QNetworkAccessManager* netManager = new QNetworkAccessManager();
	if (!netManager) {
		qDebug() << "Unable to create QNetworkAccessManager!";
		emit complete("Unable to create QNetworkAccessManager!", false);
		return;
	}

	// First off we initialize our NetworkManager
	netManager = new QNetworkAccessManager();
	if (!netManager) {
		qDebug() << "Unable to create QNetworkAccessManager!";
		emit complete("Unable to create QNetworkAccessManager!", false);
		return;
	}

	mQueryUri = "http://mycompany" + feedType;
	// We setup our url

	QUrl url(mQueryUri);
	QNetworkRequest req(url);

	// Setup the reply and connect to the reply function
	QNetworkReply* ipReply = netManager->get(req);
	connect(ipReply, SIGNAL(finished()), this, SLOT(onReply()));
}

void Employee::onReply() {
	QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());

	QString response;

	bool success = false;
	if (reply) {
		if (reply->error() == QNetworkReply::NoError) {

			int available = reply->bytesAvailable();
			if (available > 0) {
				int bufSize = sizeof(char) * available + sizeof(char);
				QByteArray buffer(bufSize, 0);
				int read = reply->read(buffer.data(), available);
				response = QString(buffer);
				success = true;
				Q_UNUSED(read);
			}
		} else {
			response =
					QString("Error: ") + reply->errorString()
							+ QString(" status:")
							+ reply->attribute(
									QNetworkRequest::HttpStatusCodeAttribute).toString();
			qDebug() << response;
		}
		reply->deleteLater();
	}
	if (response.trimmed().isEmpty()) {
		response = "Request failed. Check internet connection";
		qDebug() << response;
	}

	bb::cascades::GroupDataModel* dm = new bb::cascades::GroupDataModel(
			QStringList() << "id");

	dm->setGrouping(bb::cascades::ItemGrouping::None);

	// parse the json response with JsonDataAccess
	bb::data&colon;:JsonDataAccess ja;
	QVariant jsonva = ja.loadFromBuffer(response);

	QVariantList feed = jsonva.toMap()["employee"].toList();

	QVariantMap player;

	foreach (QVariant v, feed)
	{
		QVariantMap feedData = v.toMap();
		dm->insert(feedData);
	}

	mListView->setDataModel(dm);
	mListView->setVisible(true);
}

 

Ok so here's my question. I load a listview, in my main, that call another qml, detailview. Since my cpp is loading my json file how can I send what I receive in my datamodel to my detailfeed so I can change let's say the label with objectName: "firstname" to change the text for datamodel.name ?

 

Hope I'm clear enough.

 

Thanks

Please use plain text.
Developer
javayoung
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: How to send data from c++ to qml to another qml.

I think that you can access ListItemData in detailView.qml. Say ListItemData.id.

 

Why do you only add one mapping into groupdata. I think that you should add all the necessary fields, such as firstname and so on. Right?

 

bb::cascades::GroupDataModel* dm = new bb::cascades::GroupDataModel(
			QStringList() << "id");



p(^_^)q
Good good study, day day up
Please use plain text.
Trusted Contributor
AllSystemGo
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: How to send data from c++ to qml to another qml.

This is for grouping no?
Please use plain text.
Developer
javayoung
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: How to send data from c++ to qml to another qml.

[ Edited ]

If you have seen the sample as below, you will find those are keys in the groupdatamodel.

 

This link will help you to understand how to integrate with qml and c++ for listview.

https://developer.blackberry.com/cascades/documentation/ui/lists/groupdatamodel.html

 

// Create the data model and specify the sorting keys to use
GroupDataModel *model = new GroupDataModel(QStringList() << "countryName"
                                           << "cityName");
  
// Specify the type of grouping to use for the headers in the list
model->setGrouping(ItemGrouping::ByFullValue);
  
// Create a QVariantMap and populate it with data for each item. When the data
// for an item has been populated, add the item to the data model and reuse
// the same QVariantMap for the next item.
QVariantMap map;
map["countryName"] = "Italy"; map["cityName"] = "Rome"; model->insert(map);
map["countryName"] = "Spain"; map["cityName"] = "Barcelona"; model->insert(map);
map["countryName"] = "Canada"; map["cityName"] = "Waterloo"; model->insert(map);
map["countryName"] = "Canada"; map["cityName"] = "Vancouver"; model->insert(map);
map["countryName"] = "Italy"; map["cityName"] = "Milan"; model->insert(map);
map["countryName"] = "Canada"; map["cityName"] = "Toronto"; model->insert(map);
map["countryName"] = "Spain"; map["cityName"] = "Madrid"; model->insert(map);
  
// Create a ListView and associate the data model with it
ListView *listView = new ListView();
listView->setDataModel(model);

 Then you can access ListItemData.countryName or  ListItemData.cityName in the customized item qml based on the above example.




p(^_^)q
Good good study, day day up
Please use plain text.
Trusted Contributor
AllSystemGo
Posts: 140
Registered: ‎11-23-2010
My Device: Torch 9800
My Carrier: Rogers

Re: How to send data from c++ to qml to another qml.

I have to be honest with you I couldn't wrap my head around that ListItemData would be available in my second QML but sure enough you are right doing the famous ListItemData.variable works.

Thank you
Please use plain text.
Developer
javayoung
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: How to send data from c++ to qml to another qml.

I don't know it either initially. :0)  but I have reviewed all the BB10 sample codes and find the solutions which I need. :0)




p(^_^)q
Good good study, day day up
Please use plain text.