【发布时间】:2014-12-09 15:02:47
【问题描述】:
我注意到继承 Flickable 的一种奇怪行为。
我有两个文件,Comp.qml 和 main.qml。这个想法是Comp 对象的任何子对象都将是Comp 下Flickable 的子对象。我使用默认的内容
default property alias contents: flickable.children
但结果却很奇怪。
Comp.qml
Rectangle {
id: comp;
anchors.fill: parent
default property alias contents: flickable.children; // alias a default property
property int contentHeight: comp.height;
Flickable {
id: flickable;
anchors.fill: parent;
contentHeight: comp.contentHeight;
}
}
main.qml
Rectangle {
id: root;
width: 400;
height: 400;
Comp {
contentHeight: 800;
Rectangle { // <-- this rectangle should be a child of Flickable
width: 800;
height: 800;
border.color: "black";
border.width: 5;
Component.onCompleted: console.debug(parent)
}
}
}
flickable 不起作用。
如果我尝试将它们全部放在一个文件中,它会按预期工作:
main.qml
Rectangle {
id: root;
width: 400;
height: 400;
Rectangle {
id: comp;
anchors.fill: parent
Flickable {
id: flickable;
anchors.fill: parent;
contentHeight: 800;
Rectangle { // <-- the child component
width: 800;
height: 800;
border.color: "black";
border.width: 5;
}
}
}
}
我错过了什么吗?如何将外部组件添加到Flickable?
【问题讨论】:
-
它看起来比 QML 在初始化之前将子项绑定到 flickable。如果你在
comp上执行onContentsChanged: {flickable.contentWidth},你会得到-1。但是这个Rectangle是800px 宽度。所以可能是重新育儿时的大小问题。