Important: Please read the Qt Code of Conduct -

Exposing Array of Objects to QML side

  • Qt Champions 2017

    class Fruit : public QObject {
         Q_PROPERTY(string name READ getname WRITE setname NOTIFY nameChanged) 
    class FruitData: public QObject {
       QMap<int, Fruit*> m_Fruits;
    fruitData = new FruitData;

    Exposed the FruitData object to qml.

    Window {
        visible: true;width: 500;height: 500;title: qsTr("Hello World")
        Column {
            anchors.fill: parent;spacing: 5
            Repeater {
                model : fruit.count
                Rectangle {
                    width: parent.width;height: 40;color : "#333333"
                    Row {
                        anchors.centerIn: parent;spacing: 5
                        Text {text :}
                        Text {text : fruitData.fruit.price}
                        Text {text : fruitData.fruit.color}

    C++ - I have 5 fruit objects on C++ side.
    QML - I used the repeater and created Row of text elements to display 5 fruits name.

    How can I do property binding such that
    Row-1 text should refer to array index 1 on C++ side
    Row-2 text should refer to array index 2 on C++ side
    Row-3 text should refer to array index 3 on C++ side

    I dont want to expose it as AbstractItemModel. Is there possibility to achieve this ?

  • Have you seen this?

    Personally I would always go with a QAbstractItemModel/QAbstractListModel based approach, but that's just personal preference. The above site demonstrates various ways of exposing C++ models to QML.

  • Qt Champions 2017

    Thank you for the same. Yes, Model is the way to achieve it. I have already achieve this with model. I did not want to use the Model. As I requested is there some other way exist like just with property binding.

  • @dheerendra The closest I can imagine is to register the Fruit type and return to QML a QVariantList with Fruit objects in it. Then, if I'm correct and everything goes fine, it will be a javascript array with javascript objects and can be accessed in QML: "fruitArray[index].name". But can a javascript array with objects be used as a model? I don't think so. In that case you must create a ListModel in a loop using the fruitArray data. Note: I haven't tested any of this.

Log in to reply