Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML Make a scroll through dynamic strings
Qt 6.11 is out! See what's new in the release blog

QML Make a scroll through dynamic strings

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 456 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roditu
    wrote on last edited by roditu
    #1

    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"});
                }
            }
        }
    }
    
    1 Reply Last reply
    0
    • R Offline
      R Offline
      roditu
      wrote on last edited by
      #2

      @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...

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved