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...@qt-project.org> on behalf 
of Sylvain Pointeau <sylvain.point...@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

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to