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.
    The object controller is a QObject-derivated C++ object that is passed to QML through the context like so :
    view->rootContext()->setProperty( "controler", controlerObj );
    The getObjects() method is a C++ method with the macro Q_INVOKABLE declared in the Controller class. It's supposed to return a QVariant containing a QList<QObject*>.
    The objects received are from different QObject-derivated classes (called Myclass1, MyClass2, ...). So I used QObject as the return type for getObjects().

    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 class AbstractItem that inherits from QObject and all classes MyClassX inherits from AbstractItem. Then I changed the return type of the method getObjects() to a QVariant containing a QList<AbstractItem*>. But what I receive (according to a console.log()) is QVariant(QList<AbstractItem*>) and NOT an array of AbstractItem objects.

    I'm guessing I might need to look into registering my type so QML can use it into an array but:

    1. I'm not sure if it's the solution to my problem
    2. 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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.