- Qt 6.10
- QML Types
- QtQml.Models
- Instantiator
- Qt 6.10.2 Reference Documentation

# Instantiator QML Type

Dynamically creates objects. More...

Import Statement: | `import QtQml.Models`

- List of all members, including inherited members

## Properties

- active : bool
- asynchronous : bool
- count : int
- delegate : QtQml::Component
- delegateModelAccess : enumeration `(since 6.10)`
- model : variant
- object : QtObject

## Signals

- objectAdded(int index, QtObject object)
- objectRemoved(int index, QtObject object)

## Methods

- QtObject objectAt(int index)

## Detailed Description

A Instantiator can be used to control the dynamic creation of objects, or to dynamically create multiple objects from a template.

The Instantiator element will manage the objects it creates. Those objects are parented to the Instantiator and can also be deleted by the Instantiator if the Instantiator's properties change. Objects can also be destroyed dynamically through other means, and the Instantiator will not recreate them unless the properties of the Instantiator change.

Note: Instantiator is part of QtQml.Models since version 2.14 and part of QtQml since version 2.1. Importing Instantiator via QtQml is deprecated since Qt 5.14.

## Property Documentation

active : bool

When active is true, and the delegate component is ready, the Instantiator will create objects according to the model. When active is false, no objects will be created and any previously created objects will be destroyed.

Default is true.

asynchronous : bool

When asynchronous is true the Instantiator will attempt to create objects asynchronously. This means that objects may not be available immediately, even if active is set to true.

You can use the objectAdded signal to respond to items being created.

Default is false.

count : int `[read-only]`

The number of objects the Instantiator is currently managing.

delegate : QtQml::Component `[default]`

The component used to create all objects.

Note that an extra variable, index, will be available inside instances of the delegate. This variable refers to the index of the instance inside the Instantiator, and can be used to obtain the object through the objectAt method of the Instantiator.

If this property is changed, all instances using the old delegate will be destroyed and new instances will be created using the new delegate.

delegateModelAccess : enumeration `[since 6.10]`

This property determines how delegates can access the model.

Constant | Description
--- | ---
`DelegateModel.ReadOnly` | Prohibit delegates from writing the model via either context properties, the `model` object, or required properties.
`DelegateModel.ReadWrite` | Allow delegates to write the model via either context properties, the `model` object, or required properties.
`DelegateModel.Qt5ReadWrite` | Allow delegates to write the model via the `model` object and context properties but not via required properties.

The default is `DelegateModel.Qt5ReadWrite`.

This property was introduced in Qt 6.10.

See also Models and Views in Qt Quick#Changing Model Data.

model : variant

This property can be set to any of the supported data models:

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

The type of model affects the properties that are exposed to the delegate.

Default value is 1, which creates a single delegate instance.

See also Data Models.

object : QtObject `[read-only]`

This is a reference to the first created object, intended as a convenience for the case where only one object has been created.

## Signal Documentation

objectAdded(int index, QtObject object)

This signal is emitted when an object is added to the Instantiator. The index parameter holds the index which the object has been given, and the object parameter holds the QtObject that has been added.

Note: The corresponding handler is `onObjectAdded`.

objectRemoved(int index, QtObject object)

This signal is emitted when an object is removed from the Instantiator. The index parameter holds the index which the object had been given, and the object parameter holds the QtObject that has been removed.

Do not keep a reference to object if it was created by this Instantiator, as in these cases it will be deleted shortly after the signal is handled.

Note: The corresponding handler is `onObjectRemoved`.

## Method Documentation

QtObject objectAt(int index)

Returns a reference to the object with the given index.