<?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[QML + QList&lt;QObject*&gt; won&#x27;t work with setContextProperty]]></title><description><![CDATA[<p dir="auto">I'm attempting to do something akin to <a href="https://doc.qt.io/qt-6/qtquick-modelviewsdata-cppmodels.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qtquick-modelviewsdata-cppmodels.html</a> .  Specifically I want a QList of QObject-derived instances to render in a QML ListView.</p>
<p dir="auto">If I follow the examples exactly, it works.  However, if I attempt to acquire the model through a custom <code>Session</code> object set to context, I run into issues.</p>
<p dir="auto">Specifically, the session code looks like:</p>
<pre><code>class Session : public QObject
{
    Q_OBJECT

    typedef QVariant result_type;

    Q_PROPERTY(result_type items READ items NOTIFY itemsChanged)
...
};
</code></pre>
<p dir="auto">Then effectively we do a:</p>
<pre><code>ListView {
    model: session.items
}
</code></pre>
<p dir="auto">and in main:</p>
<pre><code>    Session session;

    context-&gt;setContextProperty("session", &amp;session);

</code></pre>
<p dir="auto">I've spent around 4 hours on it now.  Everything yields a "Required property was not initialized" despite verifying there is content in <code>session.items</code></p>
<p dir="auto">I'm happy to expand the example or feel free to browse full proof of concept code here <a href="https://bitbucket.org/malachib/playground.qt/src/master/src/PGQT-43/" target="_blank" rel="noopener noreferrer nofollow ugc">https://bitbucket.org/malachib/playground.qt/src/master/src/PGQT-43/</a></p>
]]></description><link>https://forum.qt.io/topic/144293/qml-qlist-qobject-won-t-work-with-setcontextproperty</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 10:32:33 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/144293.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 10 Apr 2023 21:22:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QML + QList&lt;QObject*&gt; won&#x27;t work with setContextProperty on Fri, 21 Apr 2023 16:48:26 GMT]]></title><description><![CDATA[<p dir="auto">I have observed that if the consuming QML uses <code>modelData</code> rather than properties, things work as expected.</p>
<p dir="auto">i.e.</p>
<pre><code>delegate: RowLayout {
    required property string name
    Text { Layout.fillWidth: true; text: parent.name }
}
</code></pre>
<p dir="auto">exhibits the problem at hand, while</p>
<pre><code>delegate: RowLayout {
    Text { Layout.fillWidth: true; text: modelData.name }
}
</code></pre>
<p dir="auto">Works.</p>
<p dir="auto">According to the documentation, we expect the property version to work as well: "The QObject* is available as the modelData property. As a convenience, the properties of the object are also made available directly in the delegate's context"</p>
<p dir="auto">So, the problem is still unsolved.</p>
]]></description><link>https://forum.qt.io/post/755734</link><guid isPermaLink="true">https://forum.qt.io/post/755734</guid><dc:creator><![CDATA[Malachi]]></dc:creator><pubDate>Fri, 21 Apr 2023 16:48:26 GMT</pubDate></item></channel></rss>