<?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[Dynamically Loading Components]]></title><description><![CDATA[<p dir="auto">Ok, All of the documents seem to suggest that this should be easy, but I'm can't seem to get it.</p>
<p dir="auto">What I am trying to:<br />
I have a main view defined in QML, which contains a PathView. The look of the delegate for that PathView is defined in a separate QML document whose location is not known when constructing the main view.</p>
<p dir="auto">I have attempted to do this with the following procedure:</p>
<ol>
<li>construct a Declarative View using the QML document for the main view.</li>
<li>locate the QML document which defines the look of the delegate, and load it into a QDeclarativeComponent (giving it the engine of the existing view)</li>
<li>In the main QML, Give the PathView a locally defined delegate component which uses the Loader object to create an instance of the actual delegate component.</li>
</ol>
<p dir="auto">This doesn't work. The Loader object seems to have no idea about the existence of the separately loaded component. What am I missing?</p>
<p dir="auto">My code looks something like this:</p>
<p dir="auto">main.cpp:<br />
@<br />
view = new QDeclarativeView(QUrl("qrc:/MainView.qml"));</p>
<p dir="auto">QUrl delegateViewURL = findDelegateViewQML();</p>
<p dir="auto">QDeclarativeComponent delegateView(view, delegateViewURL);</p>
<p dir="auto">delegateView.create();<br />
@</p>
<p dir="auto">MainView.qml:<br />
@<br />
Component {<br />
id: listDelegate</p>
<pre><code>Loader { sourceComponent: delegateView }
</code></pre>
<p dir="auto">}</p>
<p dir="auto">PathView {<br />
id: pathView<br />
model: listModel<br />
delegate: listDelegate<br />
path: listPath<br />
}<br />
@</p>
<p dir="auto">delegateView.qml:<br />
@<br />
Rectangle {<br />
width: 100<br />
height: 100<br />
color: "grey"</p>
<pre><code>Component.onCompleted: { console.log("delegateView loaded!"); }
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">I expect to see a 100x100 grey square inside each PathView element, but they all come up empty. If I define the rectangle directly inside listDelegate, I get the result I want. Even defining the delegateView component inside MainView.qml and loading it with Loader {sourceComponent: delegateView} works great. But if I try to use the separately loaded document, I get the following in the console:</p>
<p dir="auto">delegateView loaded!<br />
qrc:/MainView.qml:46: ReferenceError: Can't find variable: delegateView<br />
qrc:/MainView.qml:46: ReferenceError: Can't find variable: delegateView<br />
qrc:/MainView.qml:46: ReferenceError: Can't find variable: delegateView</p>
<p dir="auto">The three errors makes sense because my model right now has three items.</p>
]]></description><link>https://forum.qt.io/topic/16575/dynamically-loading-components</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 16:24:57 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/16575.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 08 May 2012 19:09:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dynamically Loading Components on Tue, 08 May 2012 20:04:44 GMT]]></title><description><![CDATA[<p dir="auto">Good catch, thanks. But that doesn't seem to fix anything. I have discovered that adding this in main.cpp (after loading the component) will make it work:</p>
<p dir="auto">@<br />
view-&gt;engine()-&gt;rootContext()-&gt;setContextProperty("DelegateView", DelegateView);<br />
@</p>
<p dir="auto">is that supposed to be necessary? I'm fine with that if it is, just want to make sure that I'm using the API as intended, you know, being thorough and all that other good practice type stuff :-)</p>
]]></description><link>https://forum.qt.io/post/138378</link><guid isPermaLink="true">https://forum.qt.io/post/138378</guid><dc:creator><![CDATA[bmac6502]]></dc:creator><pubDate>Tue, 08 May 2012 20:04:44 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamically Loading Components on Tue, 08 May 2012 19:54:44 GMT]]></title><description><![CDATA[<p dir="auto">First character in QML component must be uppercase.</p>
<p dir="auto">See <a href="http://doc.qt.nokia.com/4.7-snapshot/qmlreusablecomponents.html#qml-define-components" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.nokia.com/4.7-snapshot/qmlreusablecomponents.html#qml-define-components</a></p>
]]></description><link>https://forum.qt.io/post/138376</link><guid isPermaLink="true">https://forum.qt.io/post/138376</guid><dc:creator><![CDATA[minimoog77]]></dc:creator><pubDate>Tue, 08 May 2012 19:54:44 GMT</pubDate></item></channel></rss>