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
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany
Accepted Solution

Access QList from QML

I retrieve a QList of Class2 objects from the database and want to access them in QML.

To do that i have registered the type:

qmlRegisterType<Class2>("com.class2", 1, 0, "Class2");

In addition my database class is published to qml:

qml->setContextProperty("persistanceService", persistenceService);

This is the method definition:

Q_INVOKABLE
QList<Class2*> getClass2List();

  In my qml i use import to make the registered type available:

import com.class2 1.0

 This is the code for accessing the QList:

onCreationCompleted: {
  var class2list = persistanceService.getClass2List();
  console.log("retrieved "+class2list .length+" elements");
}

 And i get:

 

TypeError: Result of expression 'class2list' [undefined] is not an object.

 

----------------------------------------------------------
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
Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Access QList from QML

As a test i wrote a method getFirstClass2 which returns the first Class2 object from the list, i can use that in QML.

So the issue seems to be the QList, which should be known in QML(?)
I could circumvent the issue by writing getClass2At(int index) methods, but i would prefer to access the list directly, as it is retrieved from the database.
----------------------------------------------------------
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
Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Access QList from QML

QList, and other more dynamic classes are not really used in QML. There is something called QDeclarativeListProperty though that helps you expose the list. You then access the items similar to an array like myobj[1]. It also has count and a few other helper methods.

http://qt-project.org/wiki/How_to_expose_lists_to_QML
Like all of my posts
Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Access QList from QML

yes, i found that one, but had a little hope that it could be easier.
As the list is dynamically created (read from DB) it seems difficult to use QDeclarativeListProperty, but i'll give it a try.

Quite annoying that you can easily use lists for List-fields, but not for other stuff.
----------------------------------------------------------
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
Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Access QList from QML

[ Edited ]

I have found this very simple example, which is good for my use case as i only need read access:

http://doc.qt.nokia.com/4.7-snapshot/declarative-cppextensions-referenceexamples-properties.htm

 

edit:

it works, i only had to return a pointer from my getter as the list created in the method was deleted after the getter completed.

typical c++ issue it seems, i still have some issues with that...

 

edit2:

After around two days with this issue it is finally all working. 15 Kudos for Kyle, even with me finding the stuff myself beforehand it was good to get it confirmed, and i am feeling good for having solved this mystery :smileyhappy:

----------------------------------------------------------
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
Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Access QList from QML

haha glad you got it, i fought with it for a while before i gave in and realized i either needed to try and avoid doing such things or just suck it up and do it.
Like all of my posts
Please use plain text.