[Interest] QtQuick - list of heterogeneous objects

Jérôme Godbout godboutj at amotus.ca
Tue May 29 14:55:49 CEST 2018


you can use to parse the obj:


Reapeater

{

  id: r_

  model: myObjListStr


 property var componentsObj: { ''image":  "MyImageComponent.qml", "text": "MyTextComponent.qml" ...}


  Loader

  {

    property var myObj: JSON.parse(modelData)

    source: r_.componentsObj[myObj.type]

  }

}


This will do it for each list entry and load the Component according to it's type. You can also create the  Component directly and use sourceComponent (it's more fun since it allow to parse the Component when the script load and not when the loader try to load it.

You can also go Instantiator, if you plan to change that list often or want to add some binding to the loaded object with the help of the onObjectAdded handler
http://doc.qt.io/qt-5/qml-qtqml-instantiator.html

________________________________
From: Interest <interest-bounces+godboutj=amotus.ca at qt-project.org> on behalf of Sylvain Pointeau <sylvain.pointeau at gmail.com>
Sent: May 29, 2018 5:21 AM
To: Qt Project
Subject: [Interest] QtQuick - list of heterogeneous objects

Dear all,

how would you do in Qml to display a list of heterogeneous objects from a array in json provided dynamically?

[
{type: "image", src: "xxx", text: "aaa"},
{type: "text", text: "aaa"},
{type: "circle", text: "aaa"}
]

and I have 3 Qml object definition (or class? or do we call them?) that I can use

What would be the best approach?

Best regards,
Sylvain

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180529/ad0f3d7b/attachment.html>


More information about the Interest mailing list