假设我有一个名为TestComponent.qml的组件文件,其中使用了子项的别名。该组件文件如下所示:
TestComponent.qml
输出结果为:
有没有一种方法能够使别名对于动态组件的创建也透明呢?最好不要使用C++。
TestComponent.qml
import QtQuick 2.5
Column {
default property alias children: cont.children;
Text {
text: "header"
}
Column {
id: cont
}
Text {
text: "footer"
}
}
然后我有一个主文件,在那里我实例化组件。如果通过静态组件实例化添加子组件Text,它会如预期地出现在头部和底部之间。但是,如果我动态添加子组件,则忽略children别名并将子组件添加到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)
}
}
输出结果为:
header
body
footer
body1
有没有一种方法能够使别名对于动态组件的创建也透明呢?最好不要使用C++。