Unsolved 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 objectcontroller
is 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_INVOKABLE
declared 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 usedQObject
as the return type forgetObjects()
.This part seems to work fine : I have an array of
MyClassX
objects in QML and I can later access custom properties of thoses objects.Then I wanted to add a common method for all classes
MyClassX
so I've added an abstract classAbstractItem
that inherits fromQObject
and all classesMyClassX
inherits fromAbstractItem
. Then I changed the return type of the methodgetObjects()
to aQVariant
containing aQList<AbstractItem*>
. But what I receive (according to aconsole.log()
) isQVariant(QList<AbstractItem*>)
and NOT an array ofAbstractItem
objects.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