<?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[Combining ParentChange with AnchorChanges]]></title><description><![CDATA[<p dir="auto">I'm trying to change parent and anchor at the same time, while animating the anchor change (Qt 5.0.1, Windows):</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> QtQuick 2.0</p>
<p dir="auto">Item {<br />
id: topItem<br />
width: 200; height: 200</p>
<pre><code>Rectangle {
    id: redRect
    width: 100; height: 100
    x: 20
    y: 20
    color: "red"
}

Rectangle {
    id: blueRect
    width: 50; height: 50
    color: "blue"
    anchors { top: redRect.top; left: redRect.right }

    states: State {
        name: "reparented"
        AnchorChanges   { target: blueRect; anchors.left: redRect.left; }
        ParentChange    { target: blueRect; parent: redRect; }
    }

    transitions: Transition {
        ParentAnimation { via: topItem }
        AnchorAnimation { duration: 1000 }
    }

    MouseArea {
        anchors.fill: parent;
        onClicked: blueRect.state = "reparented"
    }
}
</code></pre>
<p dir="auto">}@</p>
<p dir="auto">The blue box doesn't move to the new position immediately, but jumps to the right first, then moves to the proper position according to the animation; removing <em>ParentAnimation { via: topItem }</em> doesn't fix anything.</p>
<p dir="auto">I'm doing it because ParentAnimation is limited to x,y animation only, but I want to change the parent as well. If I try to change the parent afterwords, it doesn't work either:</p>
<p dir="auto">@Item {<br />
id: topItem<br />
width: 200; height: 200</p>
<pre><code>Rectangle {
    id: redRect
    width: 100; height: 100
    x: 20
    y: 20
    color: "red"
}

Rectangle {
    id: blueRect
    width: 50; height: 50
    color: "blue"
    anchors { top: redRect.top; left: redRect.right }

    states: State {
        name: "reparented"
        AnchorChanges   { target: blueRect; anchors.left: redRect.left; }
    }

    transitions: Transition {
        SequentialAnimation{
            AnchorAnimation { duration: 1000 }
            ScriptAction{ script: blueRect.parent = redRect }
        }
    }

    MouseArea {
        anchors.fill: parent;
        onClicked: blueRect.state = "reparented"
    }
}
</code></pre>
<p dir="auto">}@</p>
<p dir="auto">After a correct animation the blue box jumps down. If I remove <em>ScriptAction{ script: blueRect.parent = redRect }</em> it works as expected, but the parent is not changed then.</p>
<p dir="auto">I would like to ask if it is a bug or am I doing something wrong? And how should I actually do it correctly?</p>
]]></description><link>https://forum.qt.io/topic/26058/combining-parentchange-with-anchorchanges</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 16:17:49 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/26058.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Apr 2013 10:27:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Fri, 19 Apr 2013 09:42:58 GMT]]></title><description><![CDATA[<p dir="auto">Thanks maxus for posting your find! It makes sense now.</p>
]]></description><link>https://forum.qt.io/post/174429</link><guid isPermaLink="true">https://forum.qt.io/post/174429</guid><dc:creator><![CDATA[melghawi]]></dc:creator><pubDate>Fri, 19 Apr 2013 09:42:58 GMT</pubDate></item><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Fri, 19 Apr 2013 09:04:59 GMT]]></title><description><![CDATA[<p dir="auto">From the bug report's comments I understood, that the logic behind all this is the following: while items are static they store relative x/y coordinates, so the blueRect has x/y relative to the topItem; when ParentChange happens Qt simply changes the scene graph by changing the parent of blueRect to the redRect. The x/y coordinates now become relative to the redRect, and since anchors are not reevaluated at the ParentChange jump occurs.</p>
<p dir="auto">It just came to me now, as I realized that only <em>y</em> coordinate snaps, how to solve it: We have to specify all anchors again in the <em>AnchorChanges</em>! Here is the solution/workaround to the problem:</p>
<p dir="auto">@<br />
Item {<br />
id: topItem<br />
width: 200; height: 200</p>
<pre><code>Rectangle {
    id: redRect
    clip: true
    width: 100; height: 100
    x: 20
    y: 20
    color: "red"
}

Rectangle {
    id: blueRect
    width: 50; height: 50
    color: "blue"
    anchors { top: redRect.top; left: redRect.right }

    states: State {
        name: "reparented"
        AnchorChanges {
            target: blueRect;
            anchors.left: redRect.left
            anchors.top:  redRect.top
        }
    }

    transitions: Transition {
        SequentialAnimation{
            AnchorAnimation { duration: 1000 }
            ScriptAction{ script: blueRect.parent = redRect }
        }
    }

    MouseArea {
        anchors.fill: parent;
        onClicked: blueRect.state = "reparented"
    }
}
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">I've opened a suggestion "QTBUG-30772":<a href="https://bugreports.qt-project.org/browse/QTBUG-30772" target="_blank" rel="noopener noreferrer nofollow ugc">https://bugreports.qt-project.org/browse/QTBUG-30772</a> to fix this issue.</p>
]]></description><link>https://forum.qt.io/post/174422</link><guid isPermaLink="true">https://forum.qt.io/post/174422</guid><dc:creator><![CDATA[maxus]]></dc:creator><pubDate>Fri, 19 Apr 2013 09:04:59 GMT</pubDate></item><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Thu, 18 Apr 2013 09:07:23 GMT]]></title><description><![CDATA[<p dir="auto">Honestly, while trying to figure out how to work it out, I forgot why I needed re-parenting in the first place =)</p>
]]></description><link>https://forum.qt.io/post/174240</link><guid isPermaLink="true">https://forum.qt.io/post/174240</guid><dc:creator><![CDATA[maxus]]></dc:creator><pubDate>Thu, 18 Apr 2013 09:07:23 GMT</pubDate></item><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Wed, 17 Apr 2013 22:43:28 GMT]]></title><description><![CDATA[<p dir="auto">Yeah anchoring is much cleaner. You can still use anchoring. It is the re-parenting that seems to ruin the object's position. Is there any reason why you need to re-parent the object in the first place?</p>
]]></description><link>https://forum.qt.io/post/174176</link><guid isPermaLink="true">https://forum.qt.io/post/174176</guid><dc:creator><![CDATA[melghawi]]></dc:creator><pubDate>Wed, 17 Apr 2013 22:43:28 GMT</pubDate></item><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Wed, 17 Apr 2013 11:15:06 GMT]]></title><description><![CDATA[<p dir="auto">Your last code sample works, since it uses the built-in properties of ParentChange - "x" and "y", and your solution is probably as good as it can be at the moment.</p>
<p dir="auto">What I was concerned about is that using anchors seems much cleaner, than setting "x" and "y", especially if I want to draw something in the middle of something else, where it becomes really kinda ugly <em>x: parent.x + parent.width/2 - width/2</em>. But what can we do... I will probably just avoid using re-parenting =)</p>
<p dir="auto">ps: I've opened a bug report: "QTBUG-30723":<a href="https://bugreports.qt-project.org/browse/QTBUG-30723" target="_blank" rel="noopener noreferrer nofollow ugc">https://bugreports.qt-project.org/browse/QTBUG-30723</a></p>
]]></description><link>https://forum.qt.io/post/174060</link><guid isPermaLink="true">https://forum.qt.io/post/174060</guid><dc:creator><![CDATA[maxus]]></dc:creator><pubDate>Wed, 17 Apr 2013 11:15:06 GMT</pubDate></item><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Wed, 17 Apr 2013 10:47:44 GMT]]></title><description><![CDATA[<p dir="auto">This is as close as I can get it. It seems that the blueRect is anchoring to the topItem when animating and then snapping back to it's correct position.</p>
<p dir="auto">@<br />
Item {<br />
id: topItem<br />
width: 200; height: 200</p>
<pre><code>Rectangle {
    id: redRect
    width: 100; height: 100
    clip: true
    x: 20;
    y: 20
    color: "red"        
}

Rectangle {
    id: blueRect        
    width: 50; height: 50
    color: "blue"
    anchors { top: redRect.top; left: redRect.right }

    states: State {
        name: "reparented"
        ParentChange { target: blueRect; parent: redRect }
        AnchorChanges{ target: blueRect; anchors.left: redRect.left }
    }

    transitions: Transition {
        ParentAnimation { via: topItem
            AnchorAnimation { duration: 1000 }
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: blueRect.state = "reparented"
    }
}
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">I even tried changing the parent after the anchor change using a timer but that seemed to mess up the blueRect's position. I'm not quite sure what's happening here. Could be a bug.</p>
<p dir="auto">Does your project absolutely require anchors?</p>
<p dir="auto">@<br />
Item {<br />
id: topItem<br />
width: 200; height: 200</p>
<pre><code>Rectangle {
    id: redRect
    width: 100; height: 100
    clip: true
    x: 20;
    y: 20
    color: "red"
}

Rectangle {
    id: blueRect
    width: 50; height: 50
    color: "blue"
    x: redRect.x + redRect.width
    y: redRect.y

    states: State {
        name: "reparented"
        ParentChange { target: blueRect; parent: redRect; x: 0 }
    }

    transitions: Transition {
        ParentAnimation { via: topItem
            NumberAnimation { property: "x"; duration: 1000 }
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: blueRect.state = "reparented"
    }
}
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/post/174056</link><guid isPermaLink="true">https://forum.qt.io/post/174056</guid><dc:creator><![CDATA[melghawi]]></dc:creator><pubDate>Wed, 17 Apr 2013 10:47:44 GMT</pubDate></item><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Tue, 16 Apr 2013 08:43:16 GMT]]></title><description><![CDATA[<p dir="auto">Thanks moeg687. Your code indeed works, but now there is another problem: if I enable clipping on the <em>redRectangle</em>, changing parent via <em>topItem</em> doesn't work, the <em>blueRectangle</em> is still being clipped against the red one:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> QtQuick 2.0</p>
<p dir="auto">Item {<br />
id: topItem<br />
width: 200; height: 200</p>
<pre><code>    Rectangle {
            id: redRect
            width: 100; height: 100
            clip: true
            x: 20
            y: 20
            color: "red"
    }

    Rectangle {
            id: blueRect
            width: 50; height: 50
            color: "blue"
            anchors { top: redRect.top; left: redRect.right }

            states: State {
                    name: "reparented"
                    ParentChange { target: blueRect; parent: redRect; }
                    AnchorChanges { target: blueRect; anchors.left: redRect.left; }
            }

            transitions: Transition {
                    ParentAnimation { via: topItem }
                    AnchorAnimation { duration: 1000 }
            }

            MouseArea {
                    anchors.fill: parent;
                    onClicked: blueRect.state = "reparented"
           }
    }
</code></pre>
<p dir="auto">}@</p>
]]></description><link>https://forum.qt.io/post/173857</link><guid isPermaLink="true">https://forum.qt.io/post/173857</guid><dc:creator><![CDATA[maxus]]></dc:creator><pubDate>Tue, 16 Apr 2013 08:43:16 GMT</pubDate></item><item><title><![CDATA[Reply to Combining ParentChange with AnchorChanges on Tue, 16 Apr 2013 00:22:07 GMT]]></title><description><![CDATA[<p dir="auto">Try flipping the state changes around. That worked for me.</p>
<p dir="auto">@<br />
import QtQuick 2.0</p>
<p dir="auto">Item {<br />
id: topItem<br />
width: 200; height: 200</p>
<pre><code>    Rectangle {
            id: redRect
            width: 100; height: 100
            x: 20
            y: 20
            color: "red"
    }

    Rectangle {
            id: blueRect
            width: 50; height: 50
            color: "blue"
            anchors { top: redRect.top; left: redRect.right }

            states: State {
                    name: "reparented"
                    ParentChange { target: blueRect; parent: redRect; }
                    AnchorChanges { target: blueRect; anchors.left: redRect.left; }                        
            }

            transitions: Transition {
                    ParentAnimation { via: topItem }
                    AnchorAnimation { duration: 1000 }
            }

            MouseArea {
                    anchors.fill: parent;
                    onClicked: blueRect.state = "reparented"
            }
    }
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/post/173818</link><guid isPermaLink="true">https://forum.qt.io/post/173818</guid><dc:creator><![CDATA[melghawi]]></dc:creator><pubDate>Tue, 16 Apr 2013 00:22:07 GMT</pubDate></item></channel></rss>