DelegateChooser QML Type
允许视图针对模型中不同类型的项目使用不同的委托。更多...
| Import Statement: | import QtQml.Models |
| Since: | Qt 6.9 |
属性
详细说明
DelegateChooser 是一种特殊的Component 类型,专用于那些视图需要某个组件并将其用作委托的场景。 DelegateChooser 封装了一组DelegateChoice。这些选项用于确定将为模型中的每个项实例化的委托。选项的选择基于模型项的role 属性值,同时也基于索引。
当视图需要显示一组彼此差异显著的委托时,通常会使用DelegateChooser。 例如,典型的手机设置视图可能包含拨动开关、滑块、单选按钮以及其他基于各设置类型的可视化控件。在这种情况下,DelegateChooser 可以提供一种简便的方法,将不同类型的委托与每个设置相关联:
import QtQml.Models
import QtQuick
import QtQuick.Controls
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
}注意:此 类型旨在仅与TableView 以及任何基于DelegateModel 的视图透明地协同工作。内部未基于DelegateModel 的视图(包括用户自定义视图)需要显式支持此类组件,才能使其按描述正常运行。
另请参阅 DelegateChoice 。
属性文档
choices : list<DelegateChoice> [default]
选择器的 DelegateChoices 列表。
该列表被视为有序列表,其中第一个匹配的DelegateChoice 将被视图采用。
通常无需引用 `choices ` 属性,因为它是 `DelegateChooser ` 的默认属性,因此所有子项都会自动分配给该属性。
role : string
该属性存储用于确定给定模型项的委托者的角色或属性名称。
注意:对于 基于QAbstractItemModel 的模型(包括ListModel ),当模型发出角色已更改的信号时,DelegateChooser 将重新评估该选择。对于其他类型的模型,此选择仅在创建给定模型索引对应的项时进行一次。
另请参阅 DelegateChoice 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.