<?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[Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml]]></title><description><![CDATA[<p dir="auto">Hello, I need to get a list of structs into a qml list or table, but I do not understand what I am doing wrong trying to get the list output to qml.</p>
<p dir="auto">I have declared the list and struct in a namespace, so that i can declare the metatype for the struct.  This gives no errors, and I have no issues appending OurFiles structs to filesList or reading from the list itself in cpp.</p>
<p dir="auto">And there is no error from the declaration of the type or anything else, but it does not load, so I wonder, what have I missed or done wrong?</p>
<pre><code>namespace  {
    struct OurFiles {
        QString name;
        QString dateChanged;
        QString size;
    };
}

Q_DECLARE_METATYPE(OurFiles);
</code></pre>
<p dir="auto">in header class i have:</p>
<pre><code>Q_PROPERTY(QList&lt;OurFiles&gt; getFileList READ getFileListCpp)
</code></pre>
<p dir="auto">public:</p>
<pre><code>QList&lt;OurFiles&gt; filesList;
QList&lt;OurFiles&gt; getFileListCpp() {return filesList;}
</code></pre>
<p dir="auto">and in source</p>
<pre><code>OurFiles fileItem;
    fileItem.name = name;
    fileItem.size = size;
    fileItem.dateChanged = date;
    filesList.append(fileItem);

</code></pre>
<p dir="auto">In qml i try to read like this</p>
<pre><code>console.log(backendEnd.getFileList)
console.log(backendEnd.getFileList[1].name)
</code></pre>
<p dir="auto">But only get the result</p>
<pre><code>qrc:/WebServer.qml:19: TypeError: Cannot read property 'name' of undefined
qml: []
</code></pre>
<p dir="auto">After change as described in post below, i have this output</p>
<pre><code>qml: [QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, )]
qml: undefined
</code></pre>
<p dir="auto">Appreciate any help :)</p>
<p dir="auto">(Edit: backendEnd is also registred so that works fine..)</p>
]]></description><link>https://forum.qt.io/topic/114363/q_declare_metatype-issue-loading-qlist-struct-in-qml</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 18:31:32 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114363.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 03 May 2020 07:55:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sat, 16 Mar 2024 12:50:39 GMT]]></title><description><![CDATA[<p dir="auto">you must define struct  above and outside the class definition as follows</p>
<p dir="auto">struct OurFiles{<br />
Q_GADGET</p>
<p dir="auto">public:<br />
QString name;<br />
QString dateChanged;<br />
QString size;</p>
<pre><code> Q_PROPERTY(QString name MEMBER name)
 Q_PROPERTY(QString dateChanged MEMBER dateChanged)
 Q_PROPERTY(QString size MEMBER size)
</code></pre>
<p dir="auto">};</p>
<p dir="auto">Q_DECLARE_METATYPE(OurFiles)</p>
<p dir="auto">class Test : public QObject {<br />
Q_OBJECT<br />
// your code<br />
}<br />
and you must add Q_OBJECT macro to your class.</p>
]]></description><link>https://forum.qt.io/post/793486</link><guid isPermaLink="true">https://forum.qt.io/post/793486</guid><dc:creator><![CDATA[Aida Mosayebi]]></dc:creator><pubDate>Sat, 16 Mar 2024 12:50:39 GMT</pubDate></item><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sun, 03 May 2020 16:29:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kromignon">@<bdi>KroMignon</bdi></a> Yeah I understood :)</p>
<p dir="auto">I did not find any suitable examples for my case. But I will figure it out..</p>
]]></description><link>https://forum.qt.io/post/592311</link><guid isPermaLink="true">https://forum.qt.io/post/592311</guid><dc:creator><![CDATA[MEMekaniske]]></dc:creator><pubDate>Sun, 03 May 2020 16:29:40 GMT</pubDate></item><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sun, 03 May 2020 15:13:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/memekaniske">@<bdi>MEMekaniske</bdi></a> said in <a href="/post/592255">Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml</a>:</p>
<blockquote>
<p dir="auto">But it took 4-5 minutes rewriting it all to use QList&lt;QStringList&gt; and that works just fine.. Just very confusing not being able to use struct the same "normal" way..</p>
</blockquote>
<p dir="auto">QStringList is a known <em>native</em> type in QML, your struct not.<br />
You can register struct to work with signals/slots mechanisms, but not with QML. To add new type to QML, the type <strong>must</strong> be a QObject to enable QML to work with.<br />
There are plenty of examples how to do it.</p>
]]></description><link>https://forum.qt.io/post/592298</link><guid isPermaLink="true">https://forum.qt.io/post/592298</guid><dc:creator><![CDATA[KroMignon]]></dc:creator><pubDate>Sun, 03 May 2020 15:13:07 GMT</pubDate></item><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sun, 03 May 2020 10:24:23 GMT]]></title><description><![CDATA[<p dir="auto">I understand.</p>
<p dir="auto">But the struct needs to be above all else to to regisred, and to use Q_OBJECT or GADGET and set struct properties that will not be private I need to be under where I can registrer the type, and then that wont work. So I don't really get it. My real issue is probably just finding out how to register the types "anywhere"..</p>
<p dir="auto">But it took 4-5 minutes rewriting it all to use QList&lt;QStringList&gt; and that works just fine.. Just very confusing not being able to use struct the same "normal" way..</p>
<p dir="auto">Thanks though :)</p>
]]></description><link>https://forum.qt.io/post/592255</link><guid isPermaLink="true">https://forum.qt.io/post/592255</guid><dc:creator><![CDATA[MEMekaniske]]></dc:creator><pubDate>Sun, 03 May 2020 10:24:23 GMT</pubDate></item><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sun, 03 May 2020 10:17:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/memekaniske">@<bdi>MEMekaniske</bdi></a> said in <a href="/post/592251">Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml</a>:</p>
<blockquote>
<p dir="auto">Ok, so it cannot be done.</p>
</blockquote>
<p dir="auto">No the way you try to do it. QML needs QObject to work, that's the deal ;)</p>
]]></description><link>https://forum.qt.io/post/592253</link><guid isPermaLink="true">https://forum.qt.io/post/592253</guid><dc:creator><![CDATA[KroMignon]]></dc:creator><pubDate>Sun, 03 May 2020 10:17:19 GMT</pubDate></item><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sun, 03 May 2020 10:07:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kromignon">@<bdi>KroMignon</bdi></a> said in <a href="/post/592249">Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml</a>:<br />
Custom data type exchange with QML is only possible with QObject sub-classes. This is mandatory.</p>
<blockquote>
<p dir="auto">==&gt; <a href="https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html</a></p>
</blockquote>
<p dir="auto">Ok, so it cannot be done.</p>
]]></description><link>https://forum.qt.io/post/592251</link><guid isPermaLink="true">https://forum.qt.io/post/592251</guid><dc:creator><![CDATA[MEMekaniske]]></dc:creator><pubDate>Sun, 03 May 2020 10:07:22 GMT</pubDate></item><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sun, 03 May 2020 09:55:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/memekaniske">@<bdi>MEMekaniske</bdi></a> Please take time to read documentation, this will avoid you many frustrations!</p>
<p dir="auto">Custom data type exchange with QML is <strong>only possible with QObject sub-classes</strong>. This is mandatory.<br />
==&gt; <a href="https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html</a></p>
]]></description><link>https://forum.qt.io/post/592249</link><guid isPermaLink="true">https://forum.qt.io/post/592249</guid><dc:creator><![CDATA[KroMignon]]></dc:creator><pubDate>Sun, 03 May 2020 09:55:53 GMT</pubDate></item><item><title><![CDATA[Reply to Q_DECLARE_METATYPE(); issue loading QList&lt;struct&gt; in qml on Sun, 03 May 2020 09:01:06 GMT]]></title><description><![CDATA[<p dir="auto">Seems I misplaced the QList&lt;OurFiles&gt; filesList; declaration, moved it to inside the header class, but still some issues going on with loading,<br />
I have 8 files that's added, so number here adds up, but seems  that I have done something wrong in the setup of the QList/Struct as now my output is:</p>
<pre><code>console.log(backendEnd.getFileList)
console.log(backendEnd.getFileList[1].name)
</code></pre>
<pre><code>qml: [QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, ),QVariant(OurFiles, )]
qml: undefined
</code></pre>
]]></description><link>https://forum.qt.io/post/592246</link><guid isPermaLink="true">https://forum.qt.io/post/592246</guid><dc:creator><![CDATA[MEMekaniske]]></dc:creator><pubDate>Sun, 03 May 2020 09:01:06 GMT</pubDate></item></channel></rss>