I am attempting to build a ListView, where elements in the View can be expanded if the content exceeds a given constraint, in the example provided, this is simple a toggle on maximumLineCount on a Text component when the row is clicked.
When the view is originally expanded, the UI behaves as expected, however when the element is clicked again to collapse it, the Text line count correctly updates, but the parent rect is not resized to wrap the content.
Logging before and after the resize, you can see the childrenRect.height is not correctly recalculated, in fact the onChildrenRectChanged is not called when the child rect reduces in size, only when it grows for the first time.
main.qml file:
import QtQuick 1.1
Rectangle {
width: 360
height: 360
ListView {
id: listView
anchors.fill: parent
model: ListModel {
ListElement {
file: "Rect1.qml"
}
}
delegate: Item {
id: listViewDelegate
width: parent.width
height: lvLoader.height
Loader {
anchors {
right: parent.right
left: parent.left
top: parent.top
}
id: lvLoader
source: file
}
Connections {
target: lvLoader.item
ignoreUnknownSignals: true
}
}
}
}
Expandable list element, Rect1.qml:
import QtQuick 1.1
Rectangle {
id: r1
anchors {
left: parent.left
right: parent.right
}
color: "red"
height: childrenRect.height
Text {
text: "rect1\ntextline2"
id: t1
maximumLineCount: 1
}
Text {
anchors.top: t1.bottom
anchors.right: parent.right
text: "rect1right"
}
onChildrenRectChanged: {
console.log("onChildrenRectChanged")
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log("pre resize child height: " + r1.childrenRect.height)
if (t1.maximumLineCount === 1) t1.maximumLineCount = 2
else t1.maximumLineCount = 1
console.log("post resize child height: " + r1.childrenRect.height)
}
}
}
I did consider a work around where I save the collapsed view’s height prior to the first expansion, and manually set that as the view height when collapsing again in the future, but this will not work if the user changes the window size.
I am forced to use QtQuick 1.1 currently due to unfixed bugs [bugreports.qt-project.org] in QtQuick 2.
↧