Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Expose C++ to QML Nested ListElement



  • 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



  • 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
            }


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



  • 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].



  • @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 ;)



  • 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


Log in to reply