As the documentation says: http://doc.qt.io/qt-5/qml-qtqml-component.html 
<http://doc.qt.io/qt-5/qml-qtqml-component.html>
Dynamically created instances can be deleted with the destroy() method. See 
Dynamic QML Object Creation from JavaScript for more information.
So I tested you code by commenting oldParent.destroy(); and it worked just 
fine. you can set the opacity of oldParent to zero to be able to see this.

> On 20 Dec 2015, at 16:24, Peter Klosowski <klo...@gmail.com> wrote:
> 
> Hello,
> 
> I am curious as to whether the following is expected behavior or a bug:
> 
> import QtQuick 2.5
> import QtQuick.Window 2.1
> 
> Window {
>     id: window
>     width: 1280; height: 960
>     visible: true
>     
>     Component {
>         id: component
>         Rectangle {
>             width: 64; height:64
>             color: "blue"
>         }
>     }
>     
>     Item {
>         id: root
>         anchors.fill: parent
>         
>         Rectangle {
>             anchors.right: parent.right
>             width: 64; height:64
>             color: "red"
>             
>             MouseArea {
>                 anchors.fill: parent
>                 
>                 property var target: null
>                 
>                 onPressed: {
>                     if (oldParent) {
>                         // The blue rectangle is initially created as a child 
> of oldParent.
>                         target = component.createObject(oldParent, {"x": 0, 
> "y": 0});
>                     }
>                 }
>                 
>                 onReleased: {
>                     if (oldParent) {
>                         // How can I transfer ownership to newParent?
>                         // The blue rectangle should stay alive as a child of 
> newParent.
>                         // (It appears the following line is not sufficient!)
>                         target.parent = newParent;
>                         oldParent.destroy();
>                     }
>                 }
>             }
>         }
>         
>         Item { id: oldParent; anchors.fill: parent }
>         Item { id: newParent; anchors.fill: parent }
>     }
> }
> 
> Also, if there is a workaround to make this work (i.e. make the blue 
> rectangle stay after the mouse is released), I would love to hear about it. I 
> have posted about this on the forums, but I have not received a response so 
> far.
> 
> https://forum.qt.io/topic/61903/dynamically-created-item-destroyed-with-original-parent-despite-reparenting
>  
> <https://forum.qt.io/topic/61903/dynamically-created-item-destroyed-with-original-parent-despite-reparenting>
> 
> Best regards,
> kloffy
> _______________________________________________
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to