Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using Repeater inside ListModel
Forum Updated to NodeBB v4.3 + New Features

Using Repeater inside ListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 597 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    diego-qt
    wrote on last edited by diego-qt
    #1

    Hello, I am trying something seemingly simple. I need a list of number with 3 distinct parts:
    [ 15, 16, 17, 18, 18.5, 19, 19.5, 20, 20.5, 21, 21.5, 22, 22.5, 23, 24, 25, 26, 27, 28, 29, 30]

    As you can see, the first part increases by a rate of 1, then the second part by a rate of 0.5, and the last by a rate of 1 again. I know in advance the first numbers of each part (15, 18, 23) and the end of range (30). 21 elements in total.

    I chose ListModel because I want to use the list in a ListView object. So I tried something like:

    ListModel {
        Repeater {
            model: beginPartTwo - beginPartOne
            ListElement { val: beginPartOne + index } // 15 16 17
        }
        Repeater {
            model: (beginPartThree - beginPartTwo) * 2
            ListElement { val: beginPartTwo + index * 0.5 } // 18 18.5 19 19.5 20 20.5 21 21.5 22 22.5
        }
        Repeater {
            model: endOfRange - beginPartThree + 1
            ListElement { val: beginPartThree + index } // 23 24 25 26 27 28 29 30
        }
    }
    

    But I get this error:
    ListElement: cannot contain nested elements

    Can someone recommend another way to automate the creation of this list?

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method
      Use the function calls to do this.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        diego-qt
        wrote on last edited by
        #3

        Thank you! I wasn't aware of that function, it works now.
        I'll leave the code if ever it can be useful for someone else.

        Component.onCompleted: {
            var i
            for(i = beginPartOne; i < beginPartTwo; i++)
                listModel.append({"val": i})
            for(i = beginPartTwo; i < beginPartThree; i += 0.5)
                listModel.append({"val": i})
            for(i = beginPartThree; i <= endOfRange; i++)
                listModel.append({"val": i})
        }
        // each loop adds each segment below
        // 15 16 17 | 18 18.5 19 19.5 20 20.5 21 21.5 22 22.5 | 23 24 25 26 27 28 29 30
        
        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