Solved QML Repeater: limit numer of repeated items
-
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 } }
-
@jdonkov
Why not use a view, e.g.ListView
and setting thevisible
property of the elements at index N and higher equal tofalse
? -
@Diracsbracket That could be done with a
Repeater
just as well. -
@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?
-
@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
orColumn
? -
@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 likeRow
orColumn
. -
Thank you for your input. As long as
Repeater
doesn't provide a fitting property I'll set the unneeded items invisible.