<?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 add animation to dynamically created QML component from C++]]></title><description><![CDATA[<p dir="auto">I have a rectangle in rect.qml</p>
<pre><code>import QtQuick 2.0
Rectangle{
    id: myRect;
   width: 100
   height: 100
   color: "red"
}
</code></pre>
<p dir="auto">and animation in anim.qml:</p>
<pre><code>import QtQuick 2.12
ScaleAnimator {
        id: rotationAnim
        target: myRect
        from: 0
        to: 1
        duration: 2000
}
</code></pre>
<p dir="auto">I load the rectangle from c++:</p>
<pre><code>QQmlComponent component(qmlEngine, QUrl::fromLocalFile("rect.qml"));
QQmlContext* rootItemContext = QQmlEngine::contextForObject(rootWindow);
QQuickItem *object = qobject_cast&lt;QQuickItem*&gt;(component.beginCreate(rootItemContext));
if(object != nullptr) {
    QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
    object-&gt;setParentItem(rootWindow-&gt;contentItem());
    object-&gt;setParent(qmlEngine);
    }
}

component.completeCreate();
</code></pre>
<p dir="auto">I can load the animation the same way, but obviously, I get the error:</p>
<pre><code>ReferenceError: myRect is not defined
</code></pre>
<p dir="auto">I want to keep a rectangle and animation in different files. I have a set of animations and depending on my need I want to append selected animation to a rectangle.<br />
Is there any way to refer to this rectangle from the animation or any way to append an animation to an existing object?</p>
]]></description><link>https://forum.qt.io/topic/105261/dynamically-add-animation-to-dynamically-created-qml-component-from-c</link><generator>RSS for Node</generator><lastBuildDate>Sun, 17 May 2026 11:02:54 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/105261.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 23 Jul 2019 11:27:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dynamically add animation to dynamically created QML component from C++ on Tue, 23 Jul 2019 14:31:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/intruderexcluder">@<bdi>IntruderExcluder</bdi></a> exactly what I needed. Thank you very much.</p>
]]></description><link>https://forum.qt.io/post/542419</link><guid isPermaLink="true">https://forum.qt.io/post/542419</guid><dc:creator><![CDATA[ChrisTof]]></dc:creator><pubDate>Tue, 23 Jul 2019 14:31:49 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamically add animation to dynamically created QML component from C++ on Tue, 23 Jul 2019 13:01:48 GMT]]></title><description><![CDATA[<p dir="auto">Remove <code>target</code> property then and set <code>running</code> to <code>false</code> for your <code>ScaleAnimator</code>. Create Animator at C++ like Rectangle and do a bit magic like in example below:</p>
<pre><code>    QQmlComponent rect(&amp;engine, QUrl(QStringLiteral("qrc:/MyRect.qml")));
    QQuickItem* rectangle = qobject_cast&lt;QQuickItem*&gt;(rect.beginCreate(context));
    if (rectangle != nullptr) {
        auto rootItem = engine.rootObjects()[0]-&gt;property("contentItem").value&lt;QQuickItem*&gt;();
        rectangle-&gt;setParentItem(rootItem);
        QQmlComponent a1(&amp;engine, QUrl(QStringLiteral("qrc:/Anim1.qml")));
        auto animation = a1.beginCreate(context);
        rect.completeCreate();
        animation-&gt;setParent(rectangle);
        animation-&gt;setProperty("target", QVariant::fromValue(rectangle));
        a1.completeCreate();
        animation-&gt;setProperty("running", true);
    }
</code></pre>
]]></description><link>https://forum.qt.io/post/542413</link><guid isPermaLink="true">https://forum.qt.io/post/542413</guid><dc:creator><![CDATA[IntruderExcluder]]></dc:creator><pubDate>Tue, 23 Jul 2019 13:01:48 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamically add animation to dynamically created QML component from C++ on Tue, 23 Jul 2019 12:17:05 GMT]]></title><description><![CDATA[<p dir="auto">Of course, I can define ScaleAnimator inside the rectangle. I did not express myself clearly. I have animation and rectangle in separate files, because:</p>
<ul>
<li>I have a rectangle.</li>
<li>I have a set of animations.<br />
I create a rectangle and sometimes I want to append animation and sometimes I don't. Sometimes I append animation 1, and other time animation 2. I want to this dynamically.</li>
</ul>
]]></description><link>https://forum.qt.io/post/542399</link><guid isPermaLink="true">https://forum.qt.io/post/542399</guid><dc:creator><![CDATA[ChrisTof]]></dc:creator><pubDate>Tue, 23 Jul 2019 12:17:05 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamically add animation to dynamically created QML component from C++ on Tue, 23 Jul 2019 11:38:28 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/christof">@<bdi>ChrisTof</bdi></a> , you can do like this:-</p>
<pre><code>Rectangle {
    id: myRect
    width: 100
    height: 100
    color: "red"

    Anim
    {
        target: myRect
    }
}
</code></pre>
<p dir="auto"><strong>Note:-</strong> Remove target inside ScaleAnimator in anim.qml</p>
]]></description><link>https://forum.qt.io/post/542389</link><guid isPermaLink="true">https://forum.qt.io/post/542389</guid><dc:creator><![CDATA[Shrinidhi Upadhyaya]]></dc:creator><pubDate>Tue, 23 Jul 2019 11:38:28 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamically add animation to dynamically created QML component from C++ on Tue, 23 Jul 2019 11:36:58 GMT]]></title><description><![CDATA[<p dir="auto">You can define <code>ScaleAnimator</code> inside your rectacgle, like in documentation.</p>
]]></description><link>https://forum.qt.io/post/542388</link><guid isPermaLink="true">https://forum.qt.io/post/542388</guid><dc:creator><![CDATA[IntruderExcluder]]></dc:creator><pubDate>Tue, 23 Jul 2019 11:36:58 GMT</pubDate></item></channel></rss>