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
itcraps
Posts: 238
Registered: ‎12-26-2010
My Device: BlackBerry Z10
Accepted Solution

Modifying ArrayDataModel from C++

[ Edited ]

Hi, I have a ListView and an ArrayDataModel that is created/initialized in QML. I would like to modify(append, remove, etc) the ArrayDataModel from c++.

 

How do i achieve that? Anyone kind enough to provide a sample code? Thank you..

 

Container {
            id: mainContainer
            ListView {
                id: listView
                layout: StackListLayout {}

                dataModel: ArrayDataModel {
                    id: listArrayDataModel
                }
               .........
            }
 }

 

 

 

 

www.jackdevora.com
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Modifying ArrayDataModel from C++

Hi,

There are two different approaches of passing it to C++:

1) Fetch dataModel from C++ side:
Obtain a pointer to ListView using findChild. Get it's data model using dataModel() function. Cast this dataModel to ArrayDataModel* storing it in a member variable then use it's methods to append items.

2) Pass the model to C++ class from QML:
Declare Q_INVOKABLE function in C++ which takes QObject *dataModel as it's argument. Export an instance of the class where the function is declared to QML. In QML in onCreationComplete call this function and pass listView.dataModel to it. In function implementation use qobject_cast<ArrayDataModel *>(arg) to cast the argument to ArrayDataModel, check that the cast was successful then store the result in a member variable.

If you'll need help implementing the above please write which approach you've chosen and I'll elaborate more on it.

Andrey Fidrya, @zmeyc on twitter
Trusted Contributor
itcraps
Posts: 238
Registered: ‎12-26-2010
My Device: BlackBerry Z10

Re: Modifying ArrayDataModel from C++

thankssss
www.jackdevora.com
Developer
hungcotac
Posts: 84
Registered: ‎01-05-2013
My Device: BB10

Re: Modifying ArrayDataModel from C++

Can you tell about 2nd solution more specifily? An small example?