<?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[Using a Loader for a string of QML]]></title><description><![CDATA[<p dir="auto">I've been using the Loader QML element for loading screens. Now I'm trying to load a string of QML dynamically. Qt.createQmlObject enables me to do so, but I can't seem to get the Loader element to play along.</p>
<p dir="auto">Seems like Loader only takes a URL (QUrl) or component (QQmlComponent), but Qt.createQmlObject creates an object (QObject).</p>
<p dir="auto">I'm new to QML, but from my understanding components are reusable elements, similar to classes, and objects are the instances of those classes. I thus can't seem to wrap my head around why <code>Loader</code> wouldn't work with objects.</p>
<p dir="auto">How can I show a loading screen while (asynchronously) parsing and initializing a string of QML?</p>
<p dir="auto">Example QML code:</p>
<p dir="auto">@Item {<br />
Rectangle {id: content}</p>
<pre><code>    Loader {id: loader}

    Component.onCompleted: {
        var obj = Qt.createQmlObject('import QtQuick 2.3; Rectangle {}', content);

        loader.source = obj; // Throws error.
    }
}@
</code></pre>
]]></description><link>https://forum.qt.io/topic/42306/using-a-loader-for-a-string-of-qml</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 08:54:10 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/42306.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 07 Jun 2014 16:29:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Using a Loader for a string of QML on Tue, 10 Jun 2014 15:27:52 GMT]]></title><description><![CDATA[<p dir="auto">A few things that might help:</p>
<p dir="auto">It wasn't obvious to me that you can create a custom qml object just using qml in a text file. Create a qml file to define your object in the same directory as the code that calls it. The file name sets the qml object's name.<br />
It's very similar in effect to inheritance in C++.</p>
<p dir="auto">I think your creation code should be more like this:</p>
<p dir="auto">@    // dynamic object creation<br />
component = Qt.createComponent( qmlFileName );<br />
if ( component.status == 3 )    // enumeration 'Component.Error' == 3<br />
{<br />
// Error Handling<br />
console.log("Error loading component:", component.errorString());<br />
}<br />
else<br />
if ( component.status == 1 ) // enumeration 'Component.Ready' == 1<br />
finishCreation();<br />
else<br />
component.statusChanged.connect( finishCreation );@</p>
<p dir="auto">here's the function it references:</p>
<p dir="auto">@function finishCreation()<br />
{<br />
if ( component.status == 1 ) // enumeration 'Component.Ready' == 1<br />
{<br />
var disp = component.createObject( _rootItem, { "parent": _rootItem } );<br />
if (disp == null)<br />
{<br />
// Error Handling<br />
console.log( "Error loading plugin into display area" );<br />
}<br />
}<br />
else<br />
if ( component.status == 3 )    // enumeration 'Component.Error' == 3<br />
{<br />
// Error Handling<br />
console.log("Error loading component:", component.errorString() );<br />
}<br />
}<br />
@</p>
<p dir="auto">Note that this code sets the VISUAL parent of the created item. If you don't then inheriting parent properties doesn't work</p>
]]></description><link>https://forum.qt.io/post/231510</link><guid isPermaLink="true">https://forum.qt.io/post/231510</guid><dc:creator><![CDATA[jsprenkle]]></dc:creator><pubDate>Tue, 10 Jun 2014 15:27:52 GMT</pubDate></item></channel></rss>