DelegateChooser QML Type
允许视图对模型中不同类型的项目使用不同的委托。更多
Import Statement: | import QtQml.Models |
Since: | Qt 6.9 |
属性
详细说明
DelegateChooser 是一种特殊的Component 类型,用于视图需要组件并将其用作委托的情况。DelegateChooser 封装了一组DelegateChoices。这些选择用于确定将为模型中的每个项目实例化的委托。选择的执行基于模型项的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> |
选择器的 DelegateChoices 列表。
该列表被视为有序列表,其中第一个匹配的DelegateChoice 将被用作视图。
一般情况下不需要引用choices
属性,因为它是DelegateChooser 的默认属性,因此所有子项都会自动分配给该属性。
role : string |
该属性包含用于确定给定模型项的委托的角色或属性名称。
注: 对于基于QAbstractItemModel 的模型,包括ListModel ,当模型发出角色已改变的信号时,DelegateChooser 将重新评估选择。对于任何其他类型的模型,只有在创建给定模型索引的项目时才会进行一次选择。
另请参见 DelegateChoice 。
© 2025 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.