[SOLVED] Is there a way to change QTreeView Header color in pyqt5?


I want to be able to change the column header but anything that I try it doesn’t work. Below is example of what I want to change. I want to change the white of the header.



Below is the style sheet that I have currently

self.treeView.setStyleSheet("background-color: rgb(0, 153, 255);\n"
                                    #"color: rgb(0, 153, 255);\n"
                                    "alternate-background-color: rgb(0, 153, 255);\n"
                                    "border-style: solid;\n"
                                    "border-color: rgb(0, 153, 255);\n"
                                    "border-width: 2px;\n"
                                    "border-radius: 10px;")

Let me know if you need more code!


Here is a small complete application that shows how to set a header style for a QTreeView:

import sys
from PyQt5 import QtWidgets

class Widget(QtWidgets.QTreeView):
    def __init__(self):
        self.test_model = QtWidgets.QFileSystemModel(self)

app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet("QHeaderView::section { background-color: red }")
w = Widget()

For further information on styling the header see the docs.

Answered By – Passerby

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *