首页 > 解决方案 > 如何对 QML TreeView 中的行应用不同的样式

问题描述

我需要在 QML 中显示树视图。数据看起来像这样

title item
    sub item
    sub item
title item
    sub item

我需要做的是在所有标题项目而不是子项目旁边显示一个复选框

我已经设法为所有项目设置了一个复选框,但我不知道如何只为标题项目显示它

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TreeView {
        model: myModel

        alternatingRowColors: false
        anchors.fill: parent

        headerDelegate: {
            visible: false
        }

        TableViewColumn {
            title: "Name"
            role: "display"
            width: 300
        }

        itemDelegate: Item {
            id: itemId

            CheckBox {
                anchors.verticalCenter: parent.verticalCenter
                text: styleData.value.text
            }
        }

    }
}

标签: qmltreeview

解决方案


对于任何有兴趣的人,我认为这是这样做的正确方法

    itemDelegate: Loader {
        property string imageSource: model.imageSource
        property string title: model.title
        property int fontSize: model.fontSize
        property string subText: model.subText
        source: {
            switch (model.type)
            {
            case 0:
                return "TopLevelComponent.qml"
            case 1:
                return "StationComponent.qml"
            case 2:
                return "StationComponent.qml"
            }

推荐阅读