Qt Reference Documentation

QML Repeater Element

The Repeater element allows you to repeat an Item-based component using a model. More...

Inherits Item

  • List of all members, including inherited members
  • Properties

    Detailed Description

    The Repeater element is used to create a large number of similar items. Like other view elements, a Repeater has a model and a delegate: for each entry in the model, the delegate is instantiated in a context seeded with data from the model. A Repeater item is usually enclosed in a positioner element such as Row or Column to visually position the multiple delegate items created by the Repeater.

    The following Repeater creates three instances of a Rectangle item within a Row:

     import Qt 4.7
    
     Row {
         Repeater {
             model: 3
             Rectangle {
                 width: 100; height: 40
                 border.width: 1
                 color: "yellow"
             }
         }
     }

    The model of a Repeater can be any of the supported Data Models.

    Items instantiated by the Repeater are inserted, in order, as children of the Repeater's parent. The insertion starts immediately after the repeater's position in its parent stacking list. This allows a Repeater to be used inside a layout. For example, the following Repeater's items are stacked between a red rectangle and a blue rectangle:

     Row {
         Rectangle { width: 10; height: 20; color: "red" }
         Repeater {
             model: 10
             Rectangle { width: 20; height: 20; radius: 10; color: "green" }
         }
         Rectangle { width: 10; height: 20; color: "blue" }
     }

    The index and modelData properties

    The index of a delegate is exposed as an accessible index property in the delegate. Properties of the model are also available depending upon the type of Data Model.

    Here is a Repeater that uses the index property inside the instantiated items:

     Column {
         Repeater {
             model: 10
             Text { text: "I'm item " + index }
         }
     }

    Here is another Repeater that uses the modelData property to reference the data for a particular index:

     Column {
         Repeater {
             model: ["apples", "oranges", "pears"]
             Text { text: "Data: " + modelData }
         }
     }

    A Repeater item owns all items it instantiates. Removing or dynamically destroying an item created by a Repeater results in unpredictable behavior.

    Considerations when using Repeater

    The Repeater element creates all of its delegate items when the repeater is first created. This can be inefficient if there are a large number of delegate items and not all of the items are required to be visible at the same time. If this is the case, consider using other view elements like ListView (which only creates delegate items when they are scrolled into view) or use the Dynamic Object Creation methods to create items as they are required.

    Also, note that Repeater is Item-based, and can only repeat Item-derived objects. For example, it cannot be used to repeat QObjects:

     Item {
         //XXX does not work! Can't repeat QObject as it doesn't derive from Item.
         Repeater {
             model: 10
             QObject {}
         }
     }

    Property Documentation

    read-onlycount : int

    This property holds the number of items in the repeater.


    defaultdelegate : Component

    The delegate provides a template defining each item instantiated by the repeater. The index is exposed as an accessible index property. Properties of the model are also available depending upon the type of Data Model.


    model : any

    The model providing data for the repeater.

    This property can be set to any of the following:

    • A number that indicates the number of delegates to be created
    • A model (e.g. a ListModel item, or a QAbstractItemModel subclass)
    • A string list
    • An object list

    In each case, the data element and the index is exposed to each instantiated component. The index is always exposed as an accessible index property. In the case of an object or string list, the data element (of type string or object) is available as the modelData property. In the case of a Qt model, all roles are available as named properties just like in the view classes.

    As a special case the model can also be merely a number. In this case it will create that many instances of the component. They will also be assigned an index based on the order they are created.

    Models can also be created directly in QML, using a ListModel or XmlListModel.

    See also Data Models.


    X

    Thank you for giving your feedback.

    Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.