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
Highlighted
Developer
Posts: 193
Registered: ‎12-29-2010
My Device: Bold 9900
My Carrier: Rogers
Accepted Solution

Changing C++ variables from QML UI (json source and sorting keys)

[ Edited ]

Hi,

 

I have a tabbed pane with two tabs. I want to populate each tab with a dfifferent JSON (list (not XML). I've managed to populate the first tab with the first json file...by following the stampcollector sample app. All the UI is in QML but the JSON loading and setting groupdatamodel is done in C++.

 

1. Now I want to populate the 2nd tab's page with a different JSON list. How do I go about this? Create a new class or can I change the source of the JSON when the 2nd tab is triggered?

 

2. Just like in the stampcollecctor app, I'm using sorting keys. I have some action items in QML, which when pressed, I want the sorting of the lists to change. So let's say the first action sorts them A-Z, the second action Z-A, the third action by population, the 4th action by area.  How can access and change the sorting key? 

 

3. Is there anyway to sort alphabetically in reverse order....from Z-A?

 

Code that I have so far is below...just no idea where to start....

 

main.qml

import bb.cascades 1.0

TabbedPane {
    id: mainTabPage
    showTabsOnActionBar: true
    
    // Country Tab
    Tab {
        title: "Countries"
        NavigationPane {
            id: countryTabNav
            property variant _countryView;
            Page {
                Container {
                    ListView {
                        id: countryList
                        objectName: "countryList"
                        listItemComponents: [
                            ListItemComponent {
                                type: "listItem"
                                StandardListItem {
                                    title: ListItemData.country
                                    status: ListItemData.population
                                    description: ListItemData.capital
                                }
                            }
                        ]
                        
                        onTriggered: {
                            var chosenItem = dataModel.data(indexPath);
                            _countryView = chosenItem;
                            countryTabNav.deprecatedPushQmlByString("countryDetail.qml");
                        }
                    }
                    
                    //-- define tab content here
                    layout: StackLayout {
                    }
                    
                }
            }
        }
    }
}

 

app.cpp

#include "app.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Container>
#include <bb/cascades/GroupDataModel>
#include <bb/cascades/ListView>
#include <bb/cascades/NavigationPane>
#include <bb/cascades/QmlDocument>
#include <bb/data/JsonDataAccess>

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

App::App() {
	QmlDocument *qml = QmlDocument::create("main.qml");
	//-- setContextProperty expose C++ object in QML as an variable
	//-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable
	//qml->setContextProperty("app", this);

	AbstractPane *root = qml->createRootNode<AbstractPane>();

	// ListView Model
	ListView *countryList = root->findChild<ListView*>("countryList");
	setupCountryListModel(countryList);

	Application::setScene(root);
}

void App::setupCountryListModel(ListView *countryList) {
	JsonDataAccess jda;
	QVariantList mainList = jda.load("app/native/assets/countries.json").value<QVariantList>();

	if (jda.hasError()) {
		bb::data::DataAccessError error = jda.error();
		qDebug() << "JSON loading error: " << error.errorType() << ": "
				<< error.errorMessage();
		return;
	}

	GroupDataModel *countryModel = new GroupDataModel(QStringList() << "country");
	countryModel->setParent(this);
	countryModel->insertList(mainList);
	countryModel->setGrouping(ItemGrouping::ByFullValue);

	countryList->setDataModel(countryModel);

}

 

app.hpp

#ifndef APP_H
#define APP_H

#include <QObject>
#include <bb/cascades/Application>

using namespace bb::cascades;

namespace bb
{
    namespace cascades
    {
        class ListView;
    }
}

/*!
 * @brief Application GUI object
 */
class App : public QObject
{
    Q_OBJECT
public:
    App();

private:
    void setupCountryListModel(ListView *countryList);
};

#endif // ifndef APP_H

 

 

 

 

 

Developer
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: Changing C++ variables from QML UI (json source and sorting keys)

1.  You'll have to duplicate what you did for the other tab.  Check out weatherguesser which has a bunch of tabs and datamodels.

 

2. Checkout the groupdata model page

https://developer.blackberry.com/cascades/reference/bb__cascades__groupdatamodel.html#setsortingkeys

 

try:

countryModel->setSortingKeys())

 

3.

countryModel->setsortedascending(false)

---
If you find my post helpful please "like" it and "accept as a solution"
Retired
Posts: 138
Registered: ‎05-11-2012
My Device: Developer
My Carrier: Developer

Re: Changing C++ variables from QML UI (json source and sorting keys)

Did Dredvard's suggestions work for you?

 

Graham

Developer
Posts: 193
Registered: ‎12-29-2010
My Device: Bold 9900
My Carrier: Rogers

Re: Changing C++ variables from QML UI (json source and sorting keys)

Sorry for the late reply. Family matters. 

Back to programming now.

 

Dredvard's reply did help. Thanks. I'll mark it as solved.