Different delegates for QML ListView

前端 未结 6 859
自闭症患者
自闭症患者 2020-12-13 19:36

I would like to know if it\'s possible to use (several) different delegates for a QML ListView.

Depending on the individual object in the ListVie

6条回答
  •  猫巷女王i
    2020-12-13 19:57

    The simplest way to do this now is using DelegateChooser. This also allows you to edit the properties of the delegates, which is something that is more difficult to do with Loader!

    Example inspired from the docs:

    import QtQuick 2.14
    import QtQuick.Controls 2.14
    import Qt.labs.qmlmodels 1.0
    
    ListView {
        width: 640; height: 480
    
        ListModel {
            id: contactsModel
        ListElement {
            name: "Bill Smith"
            position: "Engineer"
        }
        ListElement {
            name: "John Brown"
            position: "Engineer"
        }
        ListElement {
            name: "Sam Wise"
            position: "Manager"
        }
       }
    
        DelegateChooser {
            id: chooser
            role: "position"
            DelegateChoice { roleValue: "Manager"; Manager { ... } }
            DelegateChoice { roleValue: "Employee"; Employee { ... } }
        }
    
        model: contractsModel
        delegate: chooser
    }
    

提交回复
热议问题