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. Expose C++ to QML Nested ListElement
Forum Updated to NodeBB v4.3 + New Features

Expose C++ to QML Nested ListElement

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 1.6k 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.
  • D Offline
    D Offline
    ducdanganhit
    wrote on last edited by ducdanganhit
    #1

    Hi, I am a newbie to QML and QT.
    I have QML Nested ListElement as code below. How can i implement c++ code to use in QML?. I know how to use with non-nested listElement in QML from c++. Could you please suggest or help me this problem?
    I displayed 'name' and 'cost', but cant display 'attributes' .
    Thanks you very much.

    import QtQuick 2.0
    
    ListModel {
        id: fruitModel
        ListElement {
            name: "Apple"
            cost: 2.45
            attributes: [
                ListElement { description: "Core" },
                ListElement { description: "Deciduous" }
            ]
        }
        ListElement {
            name: "Orange"
            cost: 3.25
            attributes: [
                ListElement { description: "Citrus" },
                ListElement { description: "Seedless" }
            ]
        }
        ListElement {
            name: "Banana"
            cost: 1.95
            attributes: [
                ListElement { description: "Tropical" },
                ListElement { description: "Seedless" }
            ]
        }
    }
    

    listmodel-nested.qml

    1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by
      #2

      Hi @ducdanganhit , here you go, please have a look at the sample code:-

       Text {
                  anchors.centerIn: parent
                  text: name + " " + cost.get(1).description
              }
      

      You can access the value of description with respect to index, the above code will give you the value
      "Seedless", if you want "Tropical" then you need to change the value, you need to make it "0", here is the sample code below

       Text {
                  anchors.centerIn: parent
                  text: name + " " + cost.get(0).description
              }
      

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      D 1 Reply Last reply
      1
      • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

        Hi @ducdanganhit , here you go, please have a look at the sample code:-

         Text {
                    anchors.centerIn: parent
                    text: name + " " + cost.get(1).description
                }
        

        You can access the value of description with respect to index, the above code will give you the value
        "Seedless", if you want "Tropical" then you need to change the value, you need to make it "0", here is the sample code below

         Text {
                    anchors.centerIn: parent
                    text: name + " " + cost.get(0).description
                }
        
        D Offline
        D Offline
        ducdanganhit
        wrote on last edited by
        #3

        hi @Shrinidhi-Upadhyaya
        Thanks for your answer. I mean that i dont know how to implement Model class in C++ to show 'attributes' into QML. I implemented to show these values of 'name' and 'cost' from C++ Model class.

        KroMignonK 1 Reply Last reply
        0
        • Shrinidhi UpadhyayaS Offline
          Shrinidhi UpadhyayaS Offline
          Shrinidhi Upadhyaya
          wrote on last edited by
          #4

          Hi @ducdanganhit , ohhh okay then you need to look into a class called "QAbstractListModel",
          you can refer the documentation for that [https://doc.qt.io/qt-5.12/qabstractitemmodel.html].

          Shrinidhi Upadhyaya.
          Upvote the answer(s) that helped you to solve the issue.

          1 Reply Last reply
          0
          • D ducdanganhit

            hi @Shrinidhi-Upadhyaya
            Thanks for your answer. I mean that i dont know how to implement Model class in C++ to show 'attributes' into QML. I implemented to show these values of 'name' and 'cost' from C++ Model class.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @ducdanganhit Hi, I found for year a smart project from Thomas BOUTROUE which I use for all my QML/C++ data sharing.

            Take a look at lSuper Macros and Smart Data Model

            With those 2 projects, you can easely create a data model class from C++ and use it from QML.

            For example, PhoneBook class

            #include <QQmlConstRefPropertyHelpers.h>
            #include <QQmlObjectListModel.h>
            
            
            class PhoneContact: public Object
            {
                Q_OBJECT
            
                QML_WRITABLE_CSTREF_PROPERTY(QString, firstName)
                QML_WRITABLE_CSTREF_PROPERTY(QString, lastName)
                QML_WRITABLE_CSTREF_PROPERTY(QString, phoneNumber)
            
            public:
                explicit PhoneContact(QObject* parent=nullptr)
                    : QObject(parent)
                    , m_firstName(QString())
                    , m_lastName(QString())
                    , m_phoneNumber(QString())
                 {}
                explicit PhoneContact(const QString& firstName, const QString& lastName, const QString& phoneNumber, QObject* parent=nullptr)
                    : QObject(parent)
                    , m_firstName(firstName)
                    , m_lastName(lastName)
                    , m_phoneNumber(phoneNumber)
                 {}
            };
            
            class PhoneBook: plublic QObject
            {
                Q_OBJECT
            
                QML_OBJMODEL_PROPERTY(PhoneContact, modelContacts)
            
            public:
                explicit PhoneBook(QObject *parent=nullptr)
                    : QObject(parent)
                    , m_modelContacts(new QQmlObjectListModel<PhoneContact>(this))
                {
                    // add dummy contacts
                    m_modelContacts->append(new PhoneContact("John", "Doe", "555-55"));
                }
               ....
            }
            

            And then, you expose to QML an instance of PhoneBook as "phoneBook". Finally you can have acces to the model from QML with phoneBook.modelContacts, and each item has an attribut firstName, lastName and phoneNumber (like declared in PhoneContact class) and also the instance is accessible via attibut qtObject.

            For more details take a look at his Lightning Talk or presentation Cut development time and cost with Qt and QML

            Happy coding ;)

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            2
            • QtHelexQ Offline
              QtHelexQ Offline
              QtHelex
              wrote on last edited by
              #6

              Not sure if you found in the past 4 months a suitable solution, but I just want to let you know that I had to do something similar a while ago:
              https://forum.qt.io/topic/92015/example-updatable-model-within-a-model

              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