Package QML Type
指定已命名项目的集合。更多
Import Statement: | import QtQml.Models |
附加属性
- name : string
详细说明
包类型与DelegateModel 结合使用,可将具有共享上下文的委托提供给多个视图。
包内的任何项目都可通过Package.name 附加属性分配名称。
下面的示例创建了一个包,其中包含两个已命名的项目:列表和网格。包中的第三个项目(Rectangle )被父代到它应该出现的委托中。这允许项目在视图之间移动。
Package { Text { id: listDelegate; width: parent.width; height: 25; text: 'Empty'; Package.name: 'list' } Text { id: gridDelegate; width: parent.width / 2; height: 50; text: 'Empty'; Package.name: 'grid' } Rectangle { id: wrapper width: parent.width; height: 25 color: 'lightsteelblue' Text { text: display; anchors.centerIn: parent } state: root.upTo > index ? 'inGrid' : 'inList' states: [ State { name: 'inList' ParentChange { target: wrapper; parent: listDelegate } }, State { name: 'inGrid' ParentChange { target: wrapper; parent: gridDelegate x: 0; y: 0; width: gridDelegate.width; height: gridDelegate.height } } ] transitions: [ Transition { ParentAnimation { NumberAnimation { properties: 'x,y,width,height'; duration: 300 } } } ] } }
这些命名项被两个视图用作委托项,它们引用特殊的DelegateModel::parts 属性来选择提供所选委托项的模型。
DelegateModel { id: visualModel delegate: Delegate {} model: myModel } ListView { id: lv height: parent.height/2 width: parent.width model: visualModel.parts.list } GridView { y: parent.height/2 height: parent.height/2 width: parent.width cellWidth: width / 2 cellHeight: 50 model: visualModel.parts.grid }
注: 自 2.14 版起,Package 成为QtQml.Models 的一部分,自 2.0 版起,Package 成为QtQuick 的一部分。自 Qt XML 5.14 起,通过QtQuick 导入 Package 已被弃用。
另请参阅 Qt Quick 示例 - 视图和 Qt Qml.
附加属性文档
Package.name : string |
此附加属性用于保存包内项目的名称。
© 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.