2014-09-20 192 views

回答

13

有选择它:headerDelegate。您可以使用TableViewTableViewStyle中的那个。下面是从Base风格采取了headerDelegate上实施的实例:

import QtQuick 2.3 
import QtQuick.Window 2.2 
import QtQuick.Controls 1.2 
import QtQuick.Controls.Styles 1.2 

Window { 
    id: win 
    width: 360 
    height: 360 
    visible: true 

    ListModel { 
     id: libraryModel 
     ListElement { 
      title: "A Masterpiece" 
      author: "Gabriel" 
     } 
     ListElement { 
      title: "Brilliance" 
      author: "Jens" 
     } 
     ListElement { 
      title: "Outstanding" 
      author: "Frederik" 
     } 
    } 

    TableView { 
     TableViewColumn { 
      role: "title" 
      title: "Title" 
      width: 100 
     } 
     TableViewColumn { 
      role: "author" 
      title: "Author" 
      width: 200 
     } 
     model: libraryModel 

     style: TableViewStyle { 
      headerDelegate: Rectangle { 
       height: textItem.implicitHeight * 1.2 
       width: textItem.implicitWidth 
       color: "lightsteelblue" 
       Text { 
        id: textItem 
        anchors.fill: parent 
        verticalAlignment: Text.AlignVCenter 
        horizontalAlignment: styleData.textAlignment 
        anchors.leftMargin: 12 
        text: styleData.value 
        elide: Text.ElideRight 
        color: textColor 
        renderType: Text.NativeRendering 
       } 
       Rectangle { 
        anchors.right: parent.right 
        anchors.top: parent.top 
        anchors.bottom: parent.bottom 
        anchors.bottomMargin: 1 
        anchors.topMargin: 1 
        width: 1 
        color: "#ccc" 
       } 
      } 
     } 
    } 
} 

screenshot

正如你可能已经注意到,有一个“色彩故障”在标题的末尾(见上图)。这是因为,默认情况下,backgroundColor属性设置为white。将其更改为匹配标题颜色可解决问题,即将以下行添加到您的TableViewStyle实施中:

backgroundColor : "lightsteelblue"