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: 121
Registered: ‎11-19-2008
My Device: Not Specified
Accepted Solution

Access a list of QObjects using Javascript from QML?

I would like to access a list of QObjects from Javascript so that they can be accessed for example like this:

 

var items = mycontextproperty.getItems()
for(i =0; i< items.length; i++){
   items[i].name; 
}

I am looking to just access read-only properties.

 

In C++ I have an invokable method defined like this

Q_INVOKABLE QList getItems()

 

or like this

Q_INVOKABLE QVariantList getItems()

 

and then I manually add the QObject* items to QVariantList

 

Neither one seems to work. Does anyone know of a way to expose a simple list in QML from C++?

 

 

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Access a list of QObjects using Javascript from QML?

You can use QDeclarativeListProperty to access a list as a Q_PROPERTY.
This worked for me in some cases, but in others it did not, and i never found out why.
In these cases i just use a getSize and getElementAt(int index) method with Q_INVOKEABLE.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: Access a list of QObjects using Javascript from QML?

[ Edited ]

I think that you can implement get item data from C++ side.

 

var size = mycontextproperty.getItemSize()
for(i =0; i< size; i++){
   //name = mycontextproperty.getItemName(i); 
var data = mycontextproperty.getItem(i); //Should have a try with QVariantMap }

 

 




p(^_^)q
Good good study, day day up
Developer
Posts: 121
Registered: ‎11-19-2008
My Device: Not Specified

Re: Access a list of QObjects using Javascript from QML?

Thanks for the advice, guys! I ended up using two Q_INVOKABLE methods to iterate over the loop: getItemsCount() and then iterate using getItemAt(i).