样式缩进QML TreeView的背景颜色

时间:2016-09-21 15:32:30

标签: qt treeview qml

我想更改缩进背景颜色,如下图所示: enter image description here

到目前为止,我有类似的东西:

Component
{
  id: fileItem

  Rectangle
  {
    color: "blue"
  }
}

TreeView {
    id: view
    anchors.fill: parent
    anchors.margins: 2 * 12 + row.height
    model: fileSystemModel
    rootIndex: rootPathIndex
    selection: sel
    alternatingRowColors: false
    style: TreeViewStyle {
      branchDelegate: Rectangle {
        width: 16
        height: 16
        color: styleData.isExpanded ? "green" : "red"
      }
      frame: Rectangle {border {color: "blue"}}
      backgroundColor: "blue"
    }

    TableViewColumn {
        title: "Name"
        role: "fileName"
        resizable: true
        delegate: fileItem
    }

    onActivated:
    {
      var url = fileSystemModel.data(index, FileSystemModel.UrlStringRole)
      Qt.openUrlExternally(url)
    }
}

它基于Qt Quick Controls - File System Browser Example

1 个答案:

答案 0 :(得分:1)

要自定义TreeView中的每一行,请覆盖rowDelegate属性。例如,

TreeView {
    rowDelegate: Rectangle { color: "pink" }

    style: TreeViewStyle {
      branchDelegate: Rectangle {
        width: 16; height: 16
        color: styleData.isExpanded ? "green" : "red"
    }
    frame: Rectangle {border {color: "blue"}}
    backgroundColor: "blue"
    //...
}