Passing a QList of objects to QML
Hi, I'm a bit lost right now.
I have a QML file that calls
controller.getObjects()in order to get a list of custom objects.
controlleris a QObject-derivated C++ object that is passed to QML through the context like so :
view->rootContext()->setProperty( "controler", controlerObj );
getObjects()method is a C++ method with the macro
Q_INVOKABLEdeclared in the Controller class. It's supposed to return a QVariant containing a
The objects received are from different QObject-derivated classes (called
MyClass2, ...). So I used
QObjectas the return type for
This part seems to work fine : I have an array of
MyClassXobjects in QML and I can later access custom properties of thoses objects.
Then I wanted to add a common method for all classes
MyClassXso I've added an abstract class
AbstractItemthat inherits from
QObjectand all classes
AbstractItem. Then I changed the return type of the method
QList<AbstractItem*>. But what I receive (according to a
QVariant(QList<AbstractItem*>)and NOT an array of
I'm guessing I might need to look into registering my type so QML can use it into an array but:
- I'm not sure if it's the solution to my problem
- I can't find the doc page about it and how to do it properly in my case
So if someone could give me a hint about how to solve this (how to get an array of custom-class objects from C++ to QML), that would be nice.