Elegant
Repeater, Instantiator, and DelegateChooser
Instantiate visual items or non-visual objects for every row in a model, and pick different delegates per row with DelegateChooser.
When you have a small collection of rows and want every item created upfront inside a positioner or layout, use Repeater. When you want to create non-visual QtObject instances from a model, use Instantiator. When different rows in a Repeater or view need different delegate components based on a role's value, wrap them in a DelegateChooser.
Repeater and delegate lifecycle
A Repeater (Ljos.Ui) creates one delegate item for every row of its model. Its delegates are inserted as siblings right before the Repeater in its parent, and the Repeater itself takes up no space.
Repeater exposes model, delegate, count, itemAt(row) (which returns the delegate for row, or nothing if row is outside the model), and the signals itemAdded(index, item) and itemRemoved(index, item).
Main.qml
QML
Copy
pragma Scripts: JavaScript
import Ljos.Ui
import Ljos.Core
import Ljos.Layout

Row {
    id: root
    property string picked: "?"
    function pick(i) {
        var it = rep.itemAt(i)
        picked = it ? it.objectName : "none"
    }
    Repeater {
        id: rep
        model: 3
        onItemAdded: (index, item) => root.picked = "added " + index
        Item {
            objectName: "d" + index
            width: 10
            height: 10
        }
    }
    Component.onCompleted: pick(2)
}
Edit
Source
What this example draws
What this example draws
Nothing drawn yet.
In this example, onItemAdded runs in order for each delegate after that delegate finishes Component.onCompleted, and pick(2) reads rep.itemAt(2) to find "d2".
When the underlying model changes, Repeater updates only what is needed:
Appending or inserting a row creates a single delegate at that position.
Removing a row destroys only its delegate right after itemRemoved is emitted; removing several rows at once reports the first removed row index each time.
Moving a row moves its existing delegate without creating a new one, and changing a role value updates bindings in place without recreating the delegate.
Increasing a numeric model recreates the delegates; setting a model of 0 rows or calling clear() destroys every delegate; assigning a new model replaces every delegate.
Inside each delegate, you can access index, model, modelData (for models of plain values), and each role by name, or declare required property bindings such as required property string name, required property int index, or required property unknown modelData.
Keep these pitfalls in mind:
A Repeater is not virtualised: it creates a delegate for every row at the start. Use ListView or GridView for larger models.
If a delegate declares a required property whose name does not match any role, that delegate is not created.
When a single row is removed from a model, its delegate's index becomes -1 during removal. When an entire model is replaced, existing delegates keep their old index while being torn down.
Choosing delegates with DelegateChooser
DelegateChooser and DelegateChoice (from import QtQml.Models or import Qt.labs.qmlmodels) let a Repeater, ListView, or other view pick a different delegate component for each row. Set role on the DelegateChooser to the name of a model role, and place DelegateChoice children inside it.
For each row, DelegateChooser picks the first DelegateChoice whose roleValue matches the row's value for role (comparing numbers by value). A DelegateChoice with no roleValue (and no other constraint such as row or index) acts as a fallback and matches any row.
QML
Copy
import QtQuick
import QtQml.Models

Column {
    ListModel {
        id: lm
        ListElement { kind: "a" }
        ListElement { kind: "b" }
    }
    DelegateChooser {
        id: chooser
        role: "kind"
        DelegateChoice {
            roleValue: "a"
            Text { text: "A row " + index; color: palette.windowText }
        }
        DelegateChoice {
            Text { text: "other " + kind; color: palette.windowText }
        }
    }
    Repeater {
        model: lm
        delegate: chooser
    }
}
Here the first row matches roleValue: "a", while the second row falls through to the choice without a roleValue.
Watch out for two behaviours with DelegateChooser:
If no DelegateChoice matches a row, no delegate is created for that row, though the row is still counted in count.
Changing the value of the role named by chooser.role destroys and recreates that row's delegate so a new choice can take effect, whereas changing any other role keeps the existing delegate instance.
Creating non-visual objects with Instantiator
An Instantiator (Ljos.Models) creates one QtObject (or any non-visual or visual object) per row of its model. It provides model, delegate, count, active, object (the first object created), objectAt(row), and the objectAdded signal.
Main.qml
QML
Copy
pragma Scripts: JavaScript
import Ljos.Ui
import Ljos.Core
import Ljos.Models

Item {
    ListModel {
        id: names
        ListElement { label: "x" }
        ListElement { label: "y" }
    }
    Instantiator {
        id: inst
        model: names
        Object {
            property string shown: label
        }
    }
    Text {
        color: palette.windowText
        text: "count " + inst.count + " first " + (inst.object ? inst.object.shown : "none")
    }
}
Edit
Source
What this example draws
What this example draws
Here inst.count is 2, and inst.object.shown reads "x" from the first created Object.
When you add a row to the model, Instantiator creates one object and emits objectAdded; when you remove a row, it destroys that row's object and shifts the remaining objects up.
If you omit model, Instantiator creates a single object.
Setting active: false destroys all created objects, and setting active: true creates them again. An asynchronous Instantiator populates its objects after a moment rather than immediately.
Limits and edge cases
Repeater is not virtualised: it creates a delegate for every row.
A delegate's index becomes -1 when its row is removed.
DelegateModel with parts and rootIndex is not built.
See also
Repeater, Instantiator, and DelegateChooser
Instantiate visual items or non-visual objects for every row in a model, and pick different delegates per row with DelegateChooser.
When you have a small collection of rows and want every item created upfront inside a positioner or layout, use Repeater. When you want to create non-visual QtObject instances from a model, use Instantiator. When different rows in a Repeater or view need different delegate components based on a role's value, wrap them in a DelegateChooser.
Repeater and delegate lifecycle
A Repeater (Ljos.Ui) creates one delegate item for every row of its model. Its delegates are inserted as siblings right before the Repeater in its parent, and the Repeater itself takes up no space.
Repeater exposes model, delegate, count, itemAt(row) (which returns the delegate for row, or nothing if row is outside the model), and the signals itemAdded(index, item) and itemRemoved(index, item).
Main.qml
QML
Copy
What this example draws
What this example draws
Nothing drawn yet.
Edit
Source
pragma Scripts: JavaScript
import Ljos.Ui
import Ljos.Core
import Ljos.Layout

Row {
    id: root
    property string picked: "?"
    function pick(i) {
        var it = rep.itemAt(i)
        picked = it ? it.objectName : "none"
    }
    Repeater {
        id: rep
        model: 3
        onItemAdded: (index, item) => root.picked = "added " + index
        Item {
            objectName: "d" + index
            width: 10
            height: 10
        }
    }
    Component.onCompleted: pick(2)
}
Show all 24 lines
In this example, onItemAdded runs in order for each delegate after that delegate finishes Component.onCompleted, and pick(2) reads rep.itemAt(2) to find "d2".
When the underlying model changes, Repeater updates only what is needed:
Appending or inserting a row creates a single delegate at that position.
Removing a row destroys only its delegate right after itemRemoved is emitted; removing several rows at once reports the first removed row index each time.
Moving a row moves its existing delegate without creating a new one, and changing a role value updates bindings in place without recreating the delegate.
Increasing a numeric model recreates the delegates; setting a model of 0 rows or calling clear() destroys every delegate; assigning a new model replaces every delegate.
Inside each delegate, you can access index, model, modelData (for models of plain values), and each role by name, or declare required property bindings such as required property string name, required property int index, or required property unknown modelData.
Keep these pitfalls in mind:
A Repeater is not virtualised: it creates a delegate for every row at the start. Use ListView or GridView for larger models.
If a delegate declares a required property whose name does not match any role, that delegate is not created.
When a single row is removed from a model, its delegate's index becomes -1 during removal. When an entire model is replaced, existing delegates keep their old index while being torn down.
Choosing delegates with DelegateChooser
DelegateChooser and DelegateChoice (from import QtQml.Models or import Qt.labs.qmlmodels) let a Repeater, ListView, or other view pick a different delegate component for each row. Set role on the DelegateChooser to the name of a model role, and place DelegateChoice children inside it.
For each row, DelegateChooser picks the first DelegateChoice whose roleValue matches the row's value for role (comparing numbers by value). A DelegateChoice with no roleValue (and no other constraint such as row or index) acts as a fallback and matches any row.
QML
Copy
import QtQuick
import QtQml.Models

Column {
    ListModel {
        id: lm
        ListElement { kind: "a" }
        ListElement { kind: "b" }
    }
    DelegateChooser {
        id: chooser
        role: "kind"
        DelegateChoice {
            roleValue: "a"
            Text { text: "A row " + index; color: palette.windowText }
        }
        DelegateChoice {
            Text { text: "other " + kind; color: palette.windowText }
        }
    }
    Repeater {
        model: lm
        delegate: chooser
    }
}
Show all 25 lines
Here the first row matches roleValue: "a", while the second row falls through to the choice without a roleValue.
Watch out for two behaviours with DelegateChooser:
If no DelegateChoice matches a row, no delegate is created for that row, though the row is still counted in count.
Changing the value of the role named by chooser.role destroys and recreates that row's delegate so a new choice can take effect, whereas changing any other role keeps the existing delegate instance.
Creating non-visual objects with Instantiator
An Instantiator (Ljos.Models) creates one QtObject (or any non-visual or visual object) per row of its model. It provides model, delegate, count, active, object (the first object created), objectAt(row), and the objectAdded signal.
Main.qml
QML
Copy
What this example draws
What this example draws
Edit
Source
pragma Scripts: JavaScript
import Ljos.Ui
import Ljos.Core
import Ljos.Models

Item {
    ListModel {
        id: names
        ListElement { label: "x" }
        ListElement { label: "y" }
    }
    Instantiator {
        id: inst
        model: names
        Object {
            property string shown: label
        }
    }
    Text {
        color: palette.windowText
        text: "count " + inst.count + " first " + (inst.object ? inst.object.shown : "none")
    }
}
Show all 23 lines
Here inst.count is 2, and inst.object.shown reads "x" from the first created Object.
When you add a row to the model, Instantiator creates one object and emits objectAdded; when you remove a row, it destroys that row's object and shifts the remaining objects up.
If you omit model, Instantiator creates a single object.
Setting active: false destroys all created objects, and setting active: true creates them again. An asynchronous Instantiator populates its objects after a moment rather than immediately.
Limits and edge cases
Repeater is not virtualised: it creates a delegate for every row.
A delegate's index becomes -1 when its row is removed.
DelegateModel with parts and rootIndex is not built.
See also