<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Expose C++ to QML Nested ListElement]]></title><description><![CDATA[<p dir="auto">Hi, I am a newbie to QML and QT.<br />
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?<br />
I displayed 'name' and 'cost', but cant display 'attributes' .<br />
Thanks you very much.</p>
<pre><code>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" }
        ]
    }
}
</code></pre>
<p dir="auto"><a href="https://github.com/cedrus/qt/blob/master/qtdeclarative/src/qml/doc/snippets/qml/listmodel/listmodel-nested.qml" target="_blank" rel="noopener noreferrer nofollow ugc">listmodel-nested.qml</a></p>
]]></description><link>https://forum.qt.io/topic/100866/expose-c-to-qml-nested-listelement</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Apr 2026 14:58:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/100866.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 19 Mar 2019 09:54:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Expose C++ to QML Nested ListElement on Tue, 23 Jul 2019 10:16:42 GMT]]></title><description><![CDATA[<p dir="auto">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:<br />
<a href="https://forum.qt.io/topic/92015/example-updatable-model-within-a-model">https://forum.qt.io/topic/92015/example-updatable-model-within-a-model</a></p>
]]></description><link>https://forum.qt.io/post/542363</link><guid isPermaLink="true">https://forum.qt.io/post/542363</guid><dc:creator><![CDATA[QtHelex]]></dc:creator><pubDate>Tue, 23 Jul 2019 10:16:42 GMT</pubDate></item><item><title><![CDATA[Reply to Expose C++ to QML Nested ListElement on Wed, 20 Mar 2019 22:08:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ducdanganhit">@<bdi>ducdanganhit</bdi></a> Hi, I found for year a smart project from Thomas BOUTROUE which I use for all my QML/C++ data sharing.</p>
<p dir="auto">Take a look at <a href="http://gitlab.unique-conception.org/qt-qml-tricks/qt-supermacros" target="_blank" rel="noopener noreferrer nofollow ugc">lSuper Macros</a> and <a href="http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models" target="_blank" rel="noopener noreferrer nofollow ugc">Smart Data Model</a></p>
<p dir="auto">With those 2 projects, you can easely create a data model class from C++ and use it from QML.</p>
<p dir="auto">For example, PhoneBook class</p>
<pre><code>#include &lt;QQmlConstRefPropertyHelpers.h&gt;
#include &lt;QQmlObjectListModel.h&gt;


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&amp; firstName, const QString&amp; lastName, const QString&amp; 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&lt;PhoneContact&gt;(this))
    {
        // add dummy contacts
        m_modelContacts-&gt;append(new PhoneContact("John", "Doe", "555-55"));
    }
   ....
}
</code></pre>
<p dir="auto">And then, you expose to QML an instance of PhoneBook as "phoneBook". Finally you can have acces to the model from QML with <strong>phoneBook.modelContacts</strong>, and each item has an attribut <strong>firstName</strong>, <strong>lastName</strong> and <strong>phoneNumber</strong> (like declared in PhoneContact class) and also the instance is accessible via attibut <strong>qtObject</strong>.</p>
<p dir="auto">For more details take a look at his <a href="https://www.youtube.com/watch?v=96XAaH97XYo" target="_blank" rel="noopener noreferrer nofollow ugc">Lightning Talk</a> or presentation <a href="https://www.youtube.com/watch?v=i00OLyh0RvQ" target="_blank" rel="noopener noreferrer nofollow ugc">Cut development time and cost with Qt and QML</a></p>
<p dir="auto">Happy coding ;)</p>
]]></description><link>https://forum.qt.io/post/518484</link><guid isPermaLink="true">https://forum.qt.io/post/518484</guid><dc:creator><![CDATA[KroMignon]]></dc:creator><pubDate>Wed, 20 Mar 2019 22:08:01 GMT</pubDate></item><item><title><![CDATA[Reply to Expose C++ to QML Nested ListElement on Wed, 20 Mar 2019 05:16:09 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/ducdanganhit">@<bdi>ducdanganhit</bdi></a> , ohhh okay then you need to look into a class called "QAbstractListModel",<br />
you can refer the documentation for that [<a href="https://doc.qt.io/qt-5.12/qabstractitemmodel.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5.12/qabstractitemmodel.html</a>].</p>
]]></description><link>https://forum.qt.io/post/518438</link><guid isPermaLink="true">https://forum.qt.io/post/518438</guid><dc:creator><![CDATA[Shrinidhi Upadhyaya]]></dc:creator><pubDate>Wed, 20 Mar 2019 05:16:09 GMT</pubDate></item><item><title><![CDATA[Reply to Expose C++ to QML Nested ListElement on Tue, 19 Mar 2019 13:21:16 GMT]]></title><description><![CDATA[<p dir="auto">hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/shrinidhi-upadhyaya">@<bdi>Shrinidhi-Upadhyaya</bdi></a><br />
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.</p>
]]></description><link>https://forum.qt.io/post/518285</link><guid isPermaLink="true">https://forum.qt.io/post/518285</guid><dc:creator><![CDATA[ducdanganhit]]></dc:creator><pubDate>Tue, 19 Mar 2019 13:21:16 GMT</pubDate></item><item><title><![CDATA[Reply to Expose C++ to QML Nested ListElement on Tue, 19 Mar 2019 11:53:13 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/ducdanganhit">@<bdi>ducdanganhit</bdi></a> , here you go, please have a look at the sample code:-</p>
<pre><code> Text {
            anchors.centerIn: parent
            text: name + " " + cost.get(1).description
        }
</code></pre>
<p dir="auto">You can access the value of description with respect to index, the above code will give you the value<br />
"Seedless", if you want "Tropical" then you need to change the value, you need to make it "0", here is the sample code below</p>
<pre><code> Text {
            anchors.centerIn: parent
            text: name + " " + cost.get(0).description
        }
</code></pre>
]]></description><link>https://forum.qt.io/post/518269</link><guid isPermaLink="true">https://forum.qt.io/post/518269</guid><dc:creator><![CDATA[Shrinidhi Upadhyaya]]></dc:creator><pubDate>Tue, 19 Mar 2019 11:53:13 GMT</pubDate></item></channel></rss>