4

Let's say I have a TestComponent.qml that uses alias for child items. The component file looks like this:

TestComponent.qml

import QtQuick 2.5
Column {
    default property alias children: cont.children;

    Text {
        text: "header"
    }
    Column {
        id: cont
    }
    Text {
        text: "footer"
    }
}

Then I have a main file, where I instantiate the component. If add a child component Text via static component instantiation, it appears between header and footer as expected. However, if I add a child component dynamically, it ignores the children alias and adds the child after the footer. The file looks like this:

main.qml

import QtQuick 2.5
import QtQuick.Controls 1.4

ApplicationWindow {
    visible: true
    width: 200
    height: 200

    TestComponent {
        id: t1
        Text {
            text: "body"
        }
    }
    Component {
        id: txt
        Text {
            text: "body1"
        }
    }
    Component.onCompleted: {
        txt.createObject(t1)
    }
}

The output is:

header
body
footer
body1

Is there any way of doing it in such a way that the alias is also transparent for the dynamic component creation? Ideally without using C++.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tach
  • 663
  • 12
  • 19

1 Answers1

2

It doesn't answer the question, but the way around this could be, to make the intended parent item in component accessible via property alias and use that as a parent parameter in the createObject function call. Here is the code

TestComponent.qml

import QtQuick 2.5
Column {
    default property alias children: cont.children;
    property alias childCont: cont

    Text {
        text: "header"
    }
    Column {
        id: cont
    }
    Text {
        text: "footer"
    }
}

main.qml

import QtQuick 2.5
import QtQuick.Controls 1.4

ApplicationWindow {
    visible: true
    width: 200
    height: 200

    TestComponent {
        id: t1
        Text {
            text: "body"
        }
    }
    Component {
        id: txt
        Text {
            text: "body1"
        }
    }
    Component.onCompleted: {
        txt.createObject(t1.childCont)
    }
}

output:

header
body
body1
footer
tach
  • 663
  • 12
  • 19