<?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[setContextProperty: Best practice for having the rootContext]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I use a QAbstractListItemModel which I want to have as a member of a class. In order to have it available in QML, I need to use <code>setContextProperty</code>.</p>
<pre><code>QQmlContext *ctxt = view.rootContext();
    ctxt-&gt;setContextProperty("myModel", QVariant::fromValue(dataList));
</code></pre>
<p dir="auto">However, I wonder what the best practice is for having the rootContext available in my class. Should I just give it as argument to the constructor? Or is it better to have a function which returns my model, so that I can set the context property in a place where I have the rootContext already (e.g. the main function or a gui handler function)?</p>
<p dir="auto">Or does it make sense to make the rootContext a global variable?</p>
]]></description><link>https://forum.qt.io/topic/102285/setcontextproperty-best-practice-for-having-the-rootcontext</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 03:56:24 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/102285.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 30 Apr 2019 09:43:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to setContextProperty: Best practice for having the rootContext on Tue, 30 Apr 2019 21:27:08 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">It's not clear why you want to have access to the QML context from within your C++ class.</p>
<p dir="auto">It sounds rather like a bad idea.</p>
<p dir="auto">What does that class do except containing that model you mentioned ?</p>
]]></description><link>https://forum.qt.io/post/526258</link><guid isPermaLink="true">https://forum.qt.io/post/526258</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 30 Apr 2019 21:27:08 GMT</pubDate></item></channel></rss>