01-07-2013 11:14 AM - edited 01-07-2013 12:20 PM
I have a function in my QML file that will heavily rely on data provided from C++. All the Objects I use are :
- inhering QObject
- declared as QMETATYPE
- registered as QML types
So, let's say I have a QListaDataModel of 'A' objects that have a property called propertyA, return a QVariantList of objects 'B'
When I write this in QML :
console.log(ListItemData.propertyA), it returns me QVariant(B*). Perfect.
Now I want to access the 'propertyB' of my 'B' Object, which returns a QVariant 'C'.
console.log(ListItemData.propertyA.propertyB) will always tell me Result of expression 'ListItemData.propertyA.propertyB' [undefined] is not an object.
Why isn't this working ?
NOTE : I have already successfully used this mechanism but the dirrence was there were only QVariant and no QVariantList
NOTE 2 : If I first assign ListItemData.propertyA to a property whom Type is explicitly set to B, calling myProperty.propertyB works fine. Is there a need of cast in QML ?
01-07-2013 12:22 PM - edited 01-07-2013 01:10 PM
I have made further researching. The problem is completly related to QVariantList, it works great with just QVariant. The problem seems to be that QVariantList will store the pointers and not the objects themselves. So the JS part will not be able to access any properties or methods of the object. Weirdly, when doing exactly the same with QVariant it works. Is there an implicit conversion when using QVariant that does not occur with QVariantList ?
Here is something I have found from the Qt Documentation :
Properties that are pointers to objects or Qt interfaces are declared with the Q_PROPERTY() macro, just like other properties. The host property declaration looks like this:
Q_PROPERTY(Person *host READ host WRITE setHost)
As long as the property type, in this case Person, is registered with QML the property can be assigned.
But in my case, it never seems to work. So I have tried something else. So as not to use QVariantList, I have created a new Type, inheriting QLIst<A*>. It allows me to receive my object using the ListItemData.propertyA. But next, I can not call any method on this custom type, although it is correctly registered to QML.