Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved QML Repeater: limit numer of repeated items

    QML and Qt Quick
    3
    7
    1100
    Loading More Posts
    • 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.
    • J
      jdonkov last edited by

      Given an QML Repeater and an ObjectModel providing data for the repeater. How can I achieve that the repeater shows only the first n elements of the ObjectModel?

      The number of elements won't change dynamically.

      A possible application would be:

      Item {
          id: generic
          property int numElements: 3 // can be set from parent
      
          ObjectModel {
              id: objects
      
              Item {
                  id: item1
              }
              Item {
                  id: item2
              }
              Item {
                  id: item3
              }
          }
      
          Repeater {
              model: objects
              // shall only show numElements elements
          }
      }
      
      Diracsbracket 1 Reply Last reply Reply Quote 0
      • Diracsbracket
        Diracsbracket @jdonkov last edited by

        @jdonkov
        Why not use a view, e.g. ListView and setting the visible property of the elements at index N and higher equal to false?

        GrecKo 1 Reply Last reply Reply Quote 0
        • GrecKo
          GrecKo Qt Champions 2018 @Diracsbracket last edited by

          @Diracsbracket That could be done with a Repeater just as well.

          Diracsbracket 1 Reply Last reply Reply Quote 0
          • J
            jdonkov last edited by

            @Diracsbracket
            I thought of this as a feasible solution, too.

            Repeater {
                onItemAdded: {
                    if ((index + 1) > numElements)
                        item.visible = false
                }
            }
            

            However, the items would be "still there". Won't the invisible items still get rendered?

            Diracsbracket 1 Reply Last reply Reply Quote 0
            • Diracsbracket
              Diracsbracket @GrecKo last edited by Diracsbracket

              @GrecKo said in QML Repeater: limit numer of repeated items:

              That could be done with a Repeater just as well

              True, but the Repeater will not collapse the layout if you choose to hide an intermediate index.
              Although @jdonkov mentions he/she only wants to show the N first ones, maybe later, he may want to show another partition of N elements.

              Or a positioner element needs to be used, like Row or Column?

              1 Reply Last reply Reply Quote 0
              • Diracsbracket
                Diracsbracket @jdonkov last edited by Diracsbracket

                @jdonkov said in QML Repeater: limit numer of repeated items:

                However, the items would be "still there". Won't the invisible items still get rendered?

                Invisible items are not rendered, by definition. If you want to "collapse" their position, in that case, you could set their height to 0 instead?, or use a positioner element like Row or Column.

                1 Reply Last reply Reply Quote 0
                • J
                  jdonkov last edited by

                  Thank you for your input. As long as Repeater doesn't provide a fitting property I'll set the unneeded items invisible.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post