如何为QML TreeView背景设置渐变?

时间:2018-07-11 15:36:57

标签: qt treeview qml

我试图将TreeView的背景更改为渐变颜色,但是我什么都没找到。

TreeViewStyle中,与background相关的唯一属性是:“ backgroundColor”,但仅接收类型color

那么,可以将背景设置为渐变吗?

1 个答案:

答案 0 :(得分:1)

演示:

TreeView {
    id: tree
    TableViewColumn {
        title: "Name"
        role: "fileName"
        width: 300
    }
    TableViewColumn {
        title: "Permissions"
        role: "filePermissions"
        width: 100
    }
    model: FolderListModel {}

    Component {
        id: gradient
        LinearGradient {
            gradient: Gradient {
                GradientStop { position: 0.0; color: "white" }
                GradientStop { position: 1.0; color: "black" }
            }
        }
    }

    rowDelegate: Item { } // make rows transparent to show the background

    Component.onCompleted: {
        // the sibling of listView is the background(Rectangle) of TreeView
        var bg = tree.__listView.parent.children[1]
        // set the gradient effect to the background 
        gradient.createObject(bg, {'anchors.fill': bg})
    }
}

enter image description here