Search code examples
javascriptqtqmlqt-quick

QtQuick, QML - How do I copy one Item's child to another?


Using QML, I'd like to copy one Item's child to another. Here's what I tried:

Item
{
    id: itemOne;
    Component.onCompleted: {  children.push(itemTwo.children[0]);  }
}

Row
{   // has many children

    id: itemTwo;
    Image {}
    Image {}
    Image {}
}

I get an error like: TypeError: Result of expression 'children.push' [undefined] is not a function. So how do I copy children over then? I'd appreciate any advice

-tricky


Solution

  • An item cannot be copied because it cannot have multiple parents.

    Instead of copying, try creating the new child dynamically, as described in here, and pass 'itemTwo' as the id for the parent of the new child.