In questa pagina

Package QML Type

Indica un insieme di elementi con nome. Altro...

Import Statement: import QtQml.Models

Proprietà associate

Descrizione dettagliata

Il tipo Package viene utilizzato in combinazione con la proprietà " DelegateModel " per consentire l'assegnazione di delegati con un contesto condiviso a più viste.

A qualsiasi elemento all'interno di un pacchetto è possibile assegnare un nome tramite la proprietà associata Package.name.

L'esempio riportato di seguito crea un "Package" contenente due elementi denominati: "list" e "grid". Il terzo elemento del "Package" (l'Rectangle) è associato al delegato in cui deve apparire. Ciò consente a un elemento di spostarsi tra le diverse viste.

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

Questi elementi denominati vengono utilizzati come delegati dalle due viste che fanno riferimento alla proprietà speciale ` DelegateModel::parts ` per selezionare un modello che fornisca il delegato scelto.

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
}

Nota: il pacchetto fa parte di ` QtQml.Models ` dalla versione 2.14 e di ` QtQuick ` dalla versione 2.0. L’importazione del pacchetto tramite ` QtQuick ` è deprecata a partire da Qt 5.14.

Vedi anche Esempi diQt Quick - Viste e Qt Qml.

Documentazione sulle proprietà associate

Package.name : string [attached]

Questa proprietà associata contiene il nome di un elemento all'interno di un pacchetto.

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