3
我使用TableView
作为我在Qt5中的应用程序。可以更改此表的行的颜色(背景,文本和备用),但不能更改标题(标题)的颜色。如何更改Qt5中TableView头的颜色(背景,文本)?
如何做到这一点?
我使用TableView
作为我在Qt5中的应用程序。可以更改此表的行的颜色(背景,文本和备用),但不能更改标题(标题)的颜色。如何更改Qt5中TableView头的颜色(背景,文本)?
如何做到这一点?
有选择它:headerDelegate
。您可以使用TableView
或TableViewStyle
中的那个。下面是从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"
}
}
}
}
}
正如你可能已经注意到,有一个“色彩故障”在标题的末尾(见上图)。这是因为,默认情况下,backgroundColor
属性设置为white
。将其更改为匹配标题颜色可解决问题,即将以下行添加到您的TableViewStyle
实施中:
backgroundColor : "lightsteelblue"