I've noticed a strange behavior with inheriting Flickable
.
I have two files, Comp.qml
and main.qml
. The idea is that any children of a Comp
object will be children of the Flickable
under Comp
. I've defaulted the contents using
default property alias contents: flickable.children
but the results turn out to be strange.
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)
}
}
}
The flickable doesn't work.
If I try to put it all in one file, it work as expected :
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;
}
}
}
}
Am I missing something? How do I add an external component into a Flickable
?
As stated in the docs:
That's not the first case here, i.e. the
Rectangle
got parented to theFlickable
itself and does not expose the expected behaviour. Possibly, the addition to thechildren
does not trigger the correct parenting, in this case. As a quick fix you can re-parent the item as soon as it is added to theComp
type.To handle multiple
Item
s inside yourComp
just use a unique child (a laScrollView
). Here I modified the example to handle twoRectangle
s (just addedclip
toComp
, for aesthetic purposes).In the main:
In the
Comp.qml
file: