Passing a QList of objects to QML
-
Hi, I'm a bit lost right now.
I have a QML file that callscontroller.getObjects()in order to get a list of custom objects.
The objectcontrolleris a QObject-derivated C++ object that is passed to QML through the context like so :
view->rootContext()->setProperty( "controler", controlerObj );
ThegetObjects()method is a C++ method with the macroQ_INVOKABLEdeclared in the Controller class. It's supposed to return a QVariant containing aQList<QObject*>.
The objects received are from different QObject-derivated classes (calledMyclass1,MyClass2, ...). So I usedQObjectas the return type forgetObjects().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 classAbstractItemthat inherits fromQObjectand all classesMyClassXinherits fromAbstractItem. Then I changed the return type of the methodgetObjects()to aQVariantcontaining aQList<AbstractItem*>. But what I receive (according to aconsole.log()) isQVariant(QList<AbstractItem*>)and NOT an array ofAbstractItemobjects.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.
Thx, MoaMoaK