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. QML Repeater: limit numer of repeated items
Forum Updated to NodeBB v4.3 + New Features

QML Repeater: limit numer of repeated items

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 2.4k 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.
  • J Offline
    J Offline
    jdonkov
    wrote on last edited by
    #1

    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
        }
    }
    
    DiracsbracketD 1 Reply Last reply
    0
    • J jdonkov

      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
          }
      }
      
      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by
      #2

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

      GrecKoG 1 Reply Last reply
      0
      • DiracsbracketD Diracsbracket

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

        GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

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

        DiracsbracketD 1 Reply Last reply
        0
        • J Offline
          J Offline
          jdonkov
          wrote on last edited by
          #4

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

          DiracsbracketD 1 Reply Last reply
          0
          • GrecKoG GrecKo

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

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #5

            @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
            0
            • J jdonkov

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

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #6

              @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
              0
              • J Offline
                J Offline
                jdonkov
                wrote on last edited by
                #7

                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
                0

                • Login

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