Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Passing a QList of objects to QML
Forum Updated to NodeBB v4.3 + New Features

Passing a QList of objects to QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 581 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MoaMoaK
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved