DelegateModel QML Type | Qt 6.5
Description: Encapsulates a model and delegate.

- Qt 6.5
- QML Types
- QtQml.Models
- DelegateModel
- Qt 6.5.1 Reference Documentation

# DelegateModel QML Type

Encapsulates a model and delegate. More...

Import Statement: | import QtQml.Models

- List of all members, including inherited members

## Properties

- count : int
- delegate : Component
- filterOnGroup : string
- groups : list<DelegateModelGroup>
- items : DelegateModelGroup
- model : model
- parts : object
- persistedItems : DelegateModelGroup
- rootIndex : QModelIndex

## Attached Properties

- groups : stringlist
- inItems : bool
- inPersistedItems : bool
- isUnresolved : bool
- itemsIndex : int
- model : model
- persistedItemsIndex : int

## Methods

- QModelIndex modelIndex(int index)
- QModelIndex parentModelIndex()

## Detailed Description

The DelegateModel type encapsulates a model and the delegate that will be instantiated for items in the model.

It is usually not necessary to create a DelegateModel. However, it can be useful for manipulating and accessing the modelIndex when a QAbstractItemModel subclass is used as the model. Also, DelegateModel is used together with Package to provide delegates to multiple views, and with DelegateModelGroup to sort and filter delegate items.

DelegateModel only supports one-dimensional models – assigning a table model to DelegateModel and that to TableView will thus only show one column.

The example below illustrates using a DelegateModel with a ListView.

     import QtQuick 2.0
     import QtQml.Models 2.2

     Rectangle {
         width: 200; height: 100

         DelegateModel {
             id: visualModel
             model: ListModel {
                 ListElement { name: "Apple" }
                 ListElement { name: "Orange" }
             }
             delegate: Rectangle {
                 height: 25
                 width: 100
                 Text { text: "Name: " + name}
             }
         }

         ListView {
             anchors.fill: parent
             model: visualModel
         }
     }

## Property Documentation

count : int

delegate : Component

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

filterOnGroup : string

This property holds name of the group that is used to filter the delegate model.

Only items that belong to this group are visible to a view.

By default this is the items group.

groups : list<DelegateModelGroup>

This property holds a delegate model's group definitions.

Groups define a sub-set of the items in a delegate model and can be used to filter a model.

For every group defined in a DelegateModel two attached pseudo-properties are added to each delegate item. The first of the form DelegateModel.inGroupName holds whether the item belongs to the group and the second DelegateModel.groupNameIndex holds the index of the item in that group.

The following example illustrates using groups to select items in a model.

     import QtQuick 2.0
     import QtQml.Models 2.2

     Rectangle {
         width: 200; height: 100

         DelegateModel {
             id: visualModel
             model: ListModel {
                 ListElement { name: "Apple" }
                 ListElement { name: "Orange" }
             }

             groups: [
                 DelegateModelGroup { name: "selected" }
             ]

             delegate: Rectangle {
                 id: item
                 height: 25
                 width: 200
                 Text {
                     text: {
                         var text = "Name: " + name
                         if (item.DelegateModel.inSelected)
                             text += " (" + item.DelegateModel.selectedIndex + ")"
                         return text;
                     }
                 }
                 MouseArea {
                     anchors.fill: parent
                     onClicked: item.DelegateModel.inSelected = !item.DelegateModel.inSelected
                 }
             }
         }

         ListView {
             anchors.fill: parent
             model: visualModel
         }
     }

Warning: In contrast to normal attached properties, those cannot be set in a declarative way. The following would result in an error:

     delegate: Rectangle {
         DelegateModel.inSelected: true
     }

items : DelegateModelGroup

This property holds default group to which all new items are added.

model : model

This property holds the model providing data for the DelegateModel.

The model provides a set of data that is used to create the items for a view. For large or dynamic datasets the model is usually provided by a C++ model object. The C++ model object must be a QAbstractItemModel subclass or a simple list.

Models can also be created directly in QML, for example using ListModel.

See also Data Models.

parts : object

The parts property selects a DelegateModel which creates delegates from the part named. This is used in conjunction with the Package type.

For example, the code below selects a model which creates delegates named list from a Package:

     DelegateModel {
         id: visualModel
         delegate: Package {
             Item { Package.name: "list" }
         }
         model: myModel
     }

     ListView {
         width: 200; height:200
         model: visualModel.parts.list
     }

See also Package.

persistedItems : DelegateModelGroup

This property holds delegate model's persisted items group.

Items in this group are not destroyed when released by a view, instead they are persisted until removed from the group.

An item can be removed from the persistedItems group by setting the DelegateModel.inPersistedItems property to false. If the item is not referenced by a view at that time it will be destroyed. Adding an item to this group will not create a new instance.

Items returned by the QtQml.Models::DelegateModelGroup::create() function are automatically added to this group.

rootIndex : QModelIndex

QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. `rootIndex` allows the children of any node in a QAbstractItemModel to be provided by this model.

This property only affects models of type QAbstractItemModel that are hierarchical (e.g, a tree model).

For example, here is a simple interactive file system browser. When a directory name is clicked, the view's `rootIndex` is set to the QModelIndex node of the clicked directory, thus updating the view to show the new directory's contents.

`main.cpp`:

     int main(int argc, char ** argv)
     {
         QApplication app(argc, argv);

         QQuickView view;

         QFileSystemModel model;
         view.rootContext()->setContextProperty("fileSystemModel", &model);

         view.setSource(QUrl::fromLocalFile("view.qml"));
         view.show();

         return app.exec();
     }

`view.qml`:

     import QtQuick 2.0
     import QtQml.Models 2.2

     ListView {
         id: view
         width: 300
         height: 400

         model: DelegateModel {
             model: fileSystemModel

             delegate: Rectangle {
                 width: 200; height: 25
                 Text { text: filePath }

                 MouseArea {
                     anchors.fill: parent
                     onClicked: {
                         if (model.hasModelChildren)
                             view.model.rootIndex = view.model.modelIndex(index)
                     }
                 }
             }
         }
     }

If the model is a QAbstractItemModel subclass, the delegate can also reference a `hasModelChildren` property (optionally qualified by a model. prefix) that indicates whether the delegate's model item has any child nodes.

See also modelIndex() and parentModelIndex().

## Attached Property Documentation

DelegateModel.groups : stringlist

This attached property holds the name of DelegateModelGroups the item belongs to.

It is attached to each instance of the delegate.

DelegateModel.inItems : bool

This attached property holds whether the item belongs to the default items DelegateModelGroup.

Changing this property will add or remove the item from the items group.

It is attached to each instance of the delegate.

DelegateModel.inPersistedItems : bool

This attached property holds whether the item belongs to the persistedItems DelegateModelGroup.

Changing this property will add or remove the item from the items group. Change with caution as removing an item from the persistedItems group will destroy the current instance if it is not referenced by a model.

It is attached to each instance of the delegate.

DelegateModel.isUnresolved : bool

This attached property indicates whether the visual item is bound to a data model index. Returns true if the item is not bound to the model, and false if it is.

An unresolved item can be bound to the data model using the DelegateModelGroup::resolve() function.

It is attached to each instance of the delegate.

DelegateModel.itemsIndex : int

This attached property holds the index of the item in the default items DelegateModelGroup.

It is attached to each instance of the delegate.

DelegateModel.model : model

This attached property holds the data model this delegate instance belongs to.

It is attached to each instance of the delegate.

DelegateModel.persistedItemsIndex : int

This attached property holds the index of the item in the persistedItems DelegateModelGroup.

It is attached to each instance of the delegate.

## Method Documentation

QModelIndex modelIndex(int index)

QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. This function assists in using tree models in QML.

Returns a QModelIndex for the specified index. This value can be assigned to rootIndex.

See also rootIndex.

QModelIndex parentModelIndex()

QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. This function assists in using tree models in QML.

Returns a QModelIndex for the parent of the current rootIndex. This value can be assigned to rootIndex.

See also rootIndex.