[Interest] Dynamically created item destroyed with original parent despite reparenting
Elvis Stansvik
elvstone at gmail.com
Sun Dec 20 16:02:05 CET 2015
2015-12-20 15:51 GMT+01:00 Personal Gmail <siamak.rahimi.motem at gmail.com>:
> As the documentation says: 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.
Not sure I understand you. I think that Peter wanted to destroy the
oldParent, not just set it to transparent. So I don't think just
commenting out the .destroy() call is a solution.
What I found in http://doc.qt.io/qt-5/qml-qtquick-item.html#parent-prop is that
"Note: The concept of the visual parent differs from that of the
QObject parent. An item's visual parent may not necessarily be the
same as its object parent. See Concepts - Visual Parent in Qt Quick
for more details."
which is why Peter's assignment to parent doesn't cause it to be
reparented in the QObject sense.
Looking at http://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html
one can read:
"Any object assigned to an item's data property becomes a child of the
item within its QObject hierarchy, for memory management purposes.
Additionally, if an object added to the data property is of the Item
type, it is also assigned to the Item::children property and becomes a
child of the item within the visual scene hierarchy."
So Peter, if you just change your:
target.parent = newParent;
to
newParent.data.push(target);
Will that work? I think that should re-parent it in both the QObject
and visual scene hierarchy.
Elvis
>
> On 20 Dec 2015, at 16:24, Peter Klosowski <kloffy at 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
>
> Best regards,
> kloffy
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
More information about the Interest
mailing list