DelegateChooser QML Type | Qt 6.7
Description: Allows a view to use different delegates for different types of items in the model.

- Qt 6.7
- QML Types
- Qt.labs.qmlmodels
- DelegateChooser
- Qt 6.7.3 Reference Documentation

# DelegateChooser QML Type

Allows a view to use different delegates for different types of items in the model. More...

Import Statement: | import Qt.labs.qmlmodels

- List of all members, including inherited members

## Properties

- choices : list<DelegateChoice>
- role : string

## Detailed Description

The DelegateChooser is a special Component type intended for those scenarios where a Component is required by a view and used as a delegate. DelegateChooser encapsulates a set of DelegateChoices. These choices are used to determine the delegate that will be instantiated for each item in the model. The selection of the choice is performed based on the value that a model item has for role, and also based on index.

DelegateChooser is commonly used when a view needs to display a set of delegates that are significantly different from each other. For example, a typical phone settings view might include toggle switches, sliders, radio buttons, and other visualizations based on the type of each setting. In this case, DelegateChooser could provide an easy way to associate a different type of delegate with each setting:

     import QtQuick
     import QtQuick.Controls
     import Qt.labs.qmlmodels

     ListView {
         width: 200; height: 400

         ListModel {
             id: listModel
             ListElement { type: "info"; ... }
             ListElement { type: "switch"; ... }
             ListElement { type: "swipe"; ... }
             ListElement { type: "switch"; ... }
         }

         DelegateChooser {
             id: chooser
             role: "type"
             DelegateChoice { roleValue: "info"; ItemDelegate { ... } }
             DelegateChoice { roleValue: "switch"; SwitchDelegate { ... } }
             DelegateChoice { roleValue: "swipe"; SwipeDelegate { ... } }
         }

         model: listModel
         delegate: chooser
     }

Note: This type is intended to transparently work only with TableView and any DelegateModel-based view. Views (including user-defined views) that aren't internally based on a DelegateModel need to explicitly support this type of component to make it function as described.

See also DelegateChoice.

## Property Documentation

choices : list<DelegateChoice> `[default]`

The list of DelegateChoices for the chooser.

The list is treated as an ordered list, where the first DelegateChoice to match will be used be a view.

It should not generally be necessary to refer to the `choices` property, as it is the default property for DelegateChooser and thus all child items are automatically assigned to this property.

role : string

This property holds the role or the property name used to determine the delegate for a given model item.

Note: For QAbstractItemModel based models, including ListModel, the DelegateChooser will reevaluate the choice when the model signals that the role has changed. For any other type of model, this choice will only be done once when the item for a given model index is created.

See also DelegateChoice.