本页内容

Package QML Type

指定一组命名项。更多...

Import Statement: import QtQml.Models

附加属性

详细说明

“Package”类型与DelegateModel 配合使用,可将具有共享上下文的委托提供给多个视图。

Package 中的任何项目均可通过附着属性 `Package.name ` 指定名称。

下面的示例创建了一个包含两个命名项(list和grid)的 Package;该 Package 中的第三个项(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 版本起,它属于QtQuick 的一部分。自 Qt 5.14 起,通过QtQuick 导入 Package 已被废弃。

另请参阅 Qt Quick 示例 - 视图和 Qt Qml。

附加属性文档

Package.name : string [attached]

此附加属性存储了包中某个项的名称。

© 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.