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. Exposing Array of Objects to QML side
Forum Updated to NodeBB v4.3 + New Features

Exposing Array of Objects to QML side

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 2.8k Views
  • 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #1
    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;
    engine.rootContext()->setContextProperty("fruitData",&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 : fruitData.fruit.name}
                        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.

    Question
    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 ?

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Schluchti
      wrote on last edited by
      #2

      Have you seen this? http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html

      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.

      Want to read more about Qt?

      https://gympulsr.com/blog/qt/

      Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        E 1 Reply Last reply
        0
        • dheerendraD dheerendra

          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.

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @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.

          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