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
Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell
Accepted Solution

Maintaning one DataModel object throught multiple QML pages

[ Edited ]

Hi,

 

I have a yet simple question but so far I haven't been able to grasp the exact implementation. So I have a DataModel.cpp object which loads data from a JSON data file.

On my main qml page, the data model object is loaded by attaching it via the following code...


      // The model itself is a QListDataModel defined in 
// mydatamodel.h and registered as a type in the
// creation of the application. MyDataModel { id: dataModel // The path to the JSON file with initial data, this file
// will be moved to the data folder on the first launch
// of the application (in order to be able to get write access). jsonAssetPath: "app/native/assets/data/mydata.json" }

 
Now from the Main.qml page, I can fire methods, get / set properties, attached signals etc..  for the dataModel object. This is all fine, but in the event where my application is composed of multiple pages... ie:

Main.qml >  AddNewItem.qml
           
            On the add qml page, I add a new object to the MyDataModel object.

          > ViewItems.qml

            On the the view items, a ListView is bound to the MyDataModel object.


                > EditItem.qml

 

                  The edit page is invoked, and user apply changes to the MyDataModel

 

          > ViewGraph.qml

            On the graph page, data is striped from the MyDataModel to display a chart.

I can re-attached the MyDataModel on each pages, but this requires a full read of the JSON dataset but this is not the proper approach and leads into data refresh issue when adding, editing and deleting items.

From QML, how can a cpp object be passed from one qml page to another... I quickly tried setting a property variant on the ViewItems.qml, but this generates a qml error : Type mismatch of the value of  'dataModel' property. Expecting DataModel and found null.

Page 
{
  property variant data
  
  ...
}

 


               

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Maintaning one DataModel object throught multiple QML pages

[ Edited ]

A simpler solution might be creating the dataModel once in C++ code and injecting the instance (not the type) as a context property to QML. It will be accessible from all pages.

Regarding the code above, try declaring the property as:

property alias data : listViewId.dataModel


then you can assign it from outside of the qml:
qmlpage.data = dataModelId

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell

Re: Maintaning one DataModel object throught multiple QML pages

I did look @ the context property to QML, but then I lose the ability to attach the signal from QML.... Is there any good sample which would demonstrate the above... I've looked @ the bucket list & the weatherguesser sample... weatherguesser app does not add, update data.

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Maintaning one DataModel object throught multiple QML pages

No examples come to mind, but I think signals can be attached like this:

_app.dataModel.signalName.connect(qmlHandler)

_app.dataModel.signalName.disconnect(qmlHandler)

function qmlHandler(param) // declare signal's parameters
{
console.log("Param is: " + param);
}

Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell

Re: Maintaning one DataModel object throught multiple QML pages

Thanks, ok I have some playing arround to do...