Nesta página

Package QML Type

Especifica uma coleção de itens nomeados. Mais...

Import Statement: import QtQml.Models

Propriedades associadas

Descrição detalhada

O tipo Package é usado em conjunto com a propriedade DelegateModel para permitir que delegados com um contexto compartilhado sejam fornecidos a várias visualizações.

Qualquer item dentro de um Pacote pode receber um nome por meio da propriedade anexada ` Package.name `.

O exemplo abaixo cria um Pacote contendo dois itens nomeados: list e grid. O terceiro item do pacote (o ` Rectangle`) é vinculado ao delegado no qual deve aparecer. Isso permite que um item seja movido entre visualizações.

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 }
                }
            }
        ]
    }
}

Esses itens nomeados são usados como delegados pelas duas visualizações, que fazem referência à propriedade especial ` DelegateModel::parts ` para selecionar um modelo que forneça o delegado escolhido.

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
}

Observação: o `Package` faz parte do ` QtQml.Models ` desde a versão 2.14 e do ` QtQuick ` desde a versão 2.0. A importação do `Package` via ` QtQuick ` está obsoleta desde o Qt 5.14.

Veja também Exemplos doQt Quick - Visualizações e Qt Qml.

Documentação sobre propriedades anexadas

Package.name : string [attached]

Esta propriedade anexada contém o nome de um item dentro de um pacote.

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