10

I use TableView for my application in Qt5. It's possible to change color (background, text and alternate) of rows of this table, but there is no options for changing color of headers (titles).

How to do that?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
alr
  • 1,332
  • 3
  • 13
  • 23

2 Answers2

21

There are options for it: headerDelegate. You can use the one in TableView or TableViewStyle. Here's an example with a headerDelegate implementation taken from the Base style:

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

As you may have noticed, there is a "colour glitch" at the end of the header (see screenshot above). That is because, by default, the backgroundColor property is set to white. Changing it to match the header color solves the issue, i.e. add the following line to your TableViewStyle implementation:

backgroundColor : "lightsteelblue"
Mitch
  • 23,716
  • 9
  • 83
  • 122
  • do you have a solution where the sort indicators remain intact? – IceFire Jul 12 '19 at 20:16
  • It's been some time since I looked at this, but I would imagine that overriding `headerDelegate` means they are lost, so you would need to implement them yourself. – Mitch Jul 12 '19 at 21:04
  • Yeah, I tried everything but StyleData is not publicly available (that is what they use internally). HeaderDelegate (capital H!) is a nice helper that I ended up using. If used without properties, it already renders text. I added custom images for up/down based on control.sortIndicatorColumn and control.sortIndicatorOrder – IceFire Jul 13 '19 at 05:20
0

Mitch's example was a great basis, but it was not working entirely nice for me. The resize line was not shown properly and I also wanted to have a bottom line. This worked better for me (tested on Qt5.12):

    SystemPalette { id: myPalette; }

    style: TableViewStyle {
        backgroundColor: myPalette.base
        headerDelegate: Item {
            height: textLine.implicitHeight * 1.2 + 1

            Rectangle {
                color: myPalette.light
                height: textLine.implicitHeight * 1.2
                Text {
                    id: textLine
                    anchors.fill: parent
                    color: myPalette.text
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: styleData.textAlignment
                    text: styleData.value
                    anchors.leftMargin: 8
                    elide: Text.ElideRight
                    renderType: Text.NativeRendering
                }
            }
            Rectangle {
                anchors.left: parent.left 
                anchors.right: parent.right 
                height: 1
                y: textLine.implicitHeight * 1.2
                color: myPalette.mid 
            }
            Rectangle {
                anchors.top: parent.top 
                anchors.bottom: parent.bottom 
                width: 1
                anchors.right: parent.right
                color: myPalette.mid
            }
        }
    }

I also rely on the palette. This has the added benefit of working with macOS dark mode as well as bright mode.

Actually, mid is a bit dark for dark mode... but in dark mode, I find many contrasts a bit weak anyways in macOS, so maybe that's still fine, you need to try out yourself. Sadly, Qt's documentation of Palette again lacks any notice of which color is for borders. Maybe, it's just missing entirely.

IceFire
  • 4,016
  • 2
  • 31
  • 51