<?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[Passing a QList of structs to qml]]></title><description><![CDATA[<p dir="auto">I have a struct with the form</p>
<pre><code>struct DataEntry{
    Q_GADGET
    Q_PROPERTY(QString title READ title WRITE setTitle)
    Q_PROPERTY(QDate date READ date WRITE setDate )
    Q_PROPERTY(QTime time READ time WRITE setTime )
    Q_PROPERTY(QString note READ note WRITE setNote )
    Q_PROPERTY(QString important READ important WRITE setImportant )
public:
    QString title()const{
        return m_title;
    }
    QDate date() const{
        return m_date;
    }
    QTime time() const{
        return m_time;
    }
    QString note()  const{
        return m_note;
    }
    QString important() const{
        return m_important;
    }
public slots:
    void setTitle(QString title){
        if(m_title == title){
            return;
        }
        m_title = title;
        //emit titleChanged(title);
    }
    void setDate(QDate date){
        if(m_date == date){
            return;
        }
        m_date = date;
        //emit dateChanged(date);
    }
    void setTime(QTime time){
        if(m_time == time){
            return;
        }
        m_time = time;
        //emit  timeChanged(time);
    }
    void setNote(QString note){
        if(m_note == note){
            return;
        }
        m_note = note;
        //emit noteChanged(note);
    }
    void setImportant(QString important){
        if(m_important == important){
            return;
        }
        m_important = important;
        //emit importantChanged(important);
    }
/*signals:
    void titleChanged(QString arg);
    void dateChanged(QDate arg);
    void timeChanged(QTime arg);
    void noteChanged(QString arg);
    void importantChanged(QString arg);*/
private:
    QString m_title;
    QDate m_date;
    QTime m_time;
    QString m_note;
    QString m_important;

};
</code></pre>
<p dir="auto">Then A FileIo class to read and write to a file with the following form</p>
<pre><code>class FileIO: public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(FileIO)
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
    Q_PROPERTY(QList&lt;DataEntry&gt; memberList READ memberList WRITE setMemberList NOTIFY memberListChanged)
    Q_PROPERTY(DataEntry member READ member WRITE setMember NOTIFY memberChanged)
public:
    FileIO(QObject *parent = nullptr);
    ~FileIO();
    Q_INVOKABLE void read();
    Q_INVOKABLE void write();
    QUrl source() const;
    QList&lt;DataEntry&gt;memberList()const;
    DataEntry member()const;
public slots:
    void setSource(QUrl source);
    void setMemberList(QList&lt;DataEntry&gt; memberList);
    void setMember(DataEntry member);
signals:
    void sourceChanged(QUrl arg);
    void memberListChanged(QList&lt;DataEntry&gt; memberList);
    void memberChanged(DataEntry member);
private:
    QUrl m_source;
    QList&lt;DataEntry&gt; m_memberList;
    DataEntry m_member;
};

</code></pre>
<p dir="auto">I have a qml file which uses the following listView</p>
<pre><code>ListView{
        Component{
            id: listDelegate
            Rectangle{
                width: root.width
                height: 40
                border.color: Qt.lighter("blue")
                Rectangle{
                    id: importantRect
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    width: 30
                    height: 30
                    radius: 15
                    color: function(){
                            if(important == "Very"){
                                return "red"
                            }else if(important == "Moderate"){
                                return "yellow"
                            }else{
                                return "green"
                            }
                           }
                }
                Text {
                    id: nameText
                    text: title
                    anchors.left: importantRect.right
                    anchors.top: parent.top
                    color: "black"
                    font.pixelSize: 20
                    style: Text.Outline
                    styleColor: "lightgray"
                    elide: Text.ElideRight
                    Component.onCompleted: {
                        console.log(nameText + "from qml")
                    }
                }
                Text {
                    id: dateText
                    text: date.toLocaleString(Qt.locale(),"dd:mm:yy")
                    anchors.left: importantRect.right
                    anchors.top: nameText.bottom
                    color: "grey"
                    font.pixelSize: 15
                    style: Text.Sunken
                    styleColor: "lightgray"
                }
                Text{
                    id: timeText
                    text: time.toLocaleString(Qt.locale(),"h:m a")
                    anchors.left: dateText.right
                    anchors.top: nameText.bottom
                    color: "grey"
                    font.pixelSize: 15
                    style: Text.Sunken
                    styleColor: "lightgray"
                }
            }
        }
        model: io.memberList
        delegate: listDelegate
        anchors.fill: parent
        Component.onCompleted: {
            console.log(model.title)
        }
    }

FileIO{
            id: io
    }
</code></pre>
<p dir="auto">The io.memberList is meant to return a QList and use it to serve as a model for the ListView but this does not work, Is there a different way of using QLists to initialize models?</p>
]]></description><link>https://forum.qt.io/topic/105999/passing-a-qlist-of-structs-to-qml</link><generator>RSS for Node</generator><lastBuildDate>Sun, 11 Oct 2026 16:28:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/105999.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 17 Aug 2019 20:59:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Passing a QList of structs to qml on Sun, 18 Aug 2019 11:29:36 GMT]]></title><description><![CDATA[<p dir="auto">Okay and also one more question, If I want to use the DataEntry as a QObject child, Is the property also a pointer. i.e</p>
<pre><code>Q_PROPERTY(DataEntry* member READ member WRITE setMember NOTIFY memberChanged)

</code></pre>
]]></description><link>https://forum.qt.io/post/546646</link><guid isPermaLink="true">https://forum.qt.io/post/546646</guid><dc:creator><![CDATA[Abdulmueez]]></dc:creator><pubDate>Sun, 18 Aug 2019 11:29:36 GMT</pubDate></item><item><title><![CDATA[Reply to Passing a QList of structs to qml on Sun, 18 Aug 2019 07:08:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/abdulmueez">@<bdi>abdulmueez</bdi></a><br />
Ok, QObjects are not copyable by design<br />
so it has to be via pointer when putting them in a list.</p>
]]></description><link>https://forum.qt.io/post/546627</link><guid isPermaLink="true">https://forum.qt.io/post/546627</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 18 Aug 2019 07:08:12 GMT</pubDate></item><item><title><![CDATA[Reply to Passing a QList of structs to qml on Sun, 18 Aug 2019 00:11:11 GMT]]></title><description><![CDATA[<p dir="auto">Initially, Dataentry was a class inheriting from QObject but I was having problems using the copy constructor so, I changed it to a struct</p>
]]></description><link>https://forum.qt.io/post/546614</link><guid isPermaLink="true">https://forum.qt.io/post/546614</guid><dc:creator><![CDATA[Abdulmueez]]></dc:creator><pubDate>Sun, 18 Aug 2019 00:11:11 GMT</pubDate></item><item><title><![CDATA[Reply to Passing a QList of structs to qml on Sat, 17 Aug 2019 23:27:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/abdulmueez">@<bdi>abdulmueez</bdi></a><br />
Well you could also change DataEntry into being QObject based and try<br />
a QList&lt;DataEntry*&gt; as model input but the ItemModel gives automatic updates of the view if data changes and overall just might work better.</p>
]]></description><link>https://forum.qt.io/post/546612</link><guid isPermaLink="true">https://forum.qt.io/post/546612</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 17 Aug 2019 23:27:49 GMT</pubDate></item><item><title><![CDATA[Reply to Passing a QList of structs to qml on Sat, 17 Aug 2019 23:11:37 GMT]]></title><description><![CDATA[<p dir="auto">Alright, Thank you,<br />
I will start working on the QAbstractItemModel example incase it's not possible</p>
]]></description><link>https://forum.qt.io/post/546610</link><guid isPermaLink="true">https://forum.qt.io/post/546610</guid><dc:creator><![CDATA[Abdulmueez]]></dc:creator><pubDate>Sat, 17 Aug 2019 23:11:37 GMT</pubDate></item><item><title><![CDATA[Reply to Passing a QList of structs to qml on Sat, 17 Aug 2019 22:45:00 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
As far as i know, ListView only understands<br />
QStringList, QList&lt;QObject*&gt; or a QAbstractItemModel for a model.<br />
Not QList of some struct.</p>
<p dir="auto">So i think your best option is like the<br />
class AnimalModel : public QAbstractListModel<br />
as shown in<br />
<a href="https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html</a></p>
<p dir="auto">Disclaimer. I could be wrong and you might be able to expose the Qlist directly so<br />
wait a day or more to see if anyone has other suggestion. :=)</p>
]]></description><link>https://forum.qt.io/post/546605</link><guid isPermaLink="true">https://forum.qt.io/post/546605</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 17 Aug 2019 22:45:00 GMT</pubDate></item></channel></rss>