Qt 6.11 is out! See what's new in the release
blog
QML Make a scroll through dynamic strings
-
I have made a reproducible example, I want to be able to dynamically add couple of strings in a column and scroll through them, but this doesn't work, when i scroll down there is nothing. ContentHeight is 0, so size is 0, but if i don't set the size i won't be able to scroll down at all. How to fix this or use some different structures to achieve the same goal?
import QtQuick 2.15 import QtQuick.Controls 2.5 import QtQuick.Layouts 6.3 Page{ ListView { width: parent.width height: parent.height id: listView model: ListModel { id: recipeModel } delegate: Column { anchors.fill: parent spacing: 10 Text { Layout.fillHeight: true width: parent.width - 20 wrapMode: Text.Wrap text: model.titleText color: "white" horizontalAlignment: Text.AlignHCenter } Text { Layout.fillHeight: true width: parent.width - 20 wrapMode: Text.Wrap text: model.ingredientsText color: "orange" horizontalAlignment: Text.AlignHCenter } Text { Layout.fillHeight: true width: parent.width - 20 wrapMode: Text.Wrap text: model.instructionsText color: "blue" horizontalAlignment: Text.AlignHCenter } } ScrollBar.vertical: ScrollBar { id: scrollbar size: listView.contentHeight / listView.height policy: ScrollBar.AlwaysOn contentItem: Rectangle { implicitWidth: 5 color: "orange" radius: width } } Component.onCompleted: { var strings = [['a'.repeat(3000), 'b'.repeat(3000), 'c'.repeat(3000)], ['a'.repeat(3000), 'b'.repeat(3000), 'c'.repeat(3000)], ['a'.repeat(3000), 'b'.repeat(3000), 'c'.repeat(3000)]] for (var i = 0; i < strings.length; ++i) { recipeModel.append({titleText: strings[0] + "\n\n", ingredientsText: strings[1] + "\n\n", instructionsText: strings[2] + "\n\n"}); } } } } -
@roditu said in QML Make a scroll through dynamic strings:
anchors.fill: parent
So i found the solution by total accident:
anchors.fill: parent in delegate has to be changed to width: parent.width.QT6 is cool, QML is a literal roulette, You spin the wheel of fortune until You win. Javascript seems like heaven compared to this...