I am trying to set the background colour (color) for a) a whole QTreeView, and b) for specific rows in a QTreeView within Python.
I have found setColor and setBackgroundColor methods, but neither seem to work for me with QTreeView nor QStandardItem.
Lots of googling shows many conversations about it, but I have not been able to relate those to my code below.
Full Code is below, but two attempts to set the colour are:
InGate = QTreeView() InGate.setColor(QtGui.QColor(255, 100, 0, 255))
and
for i, d in enumerate(data): model.setItem(i, QStandardItem(d)) model.setBackgroundColor(QtGui.QColor(255, 100, i, 255))
Any help appreciated.
Thanks very much Kevin
Sorry the code example is fairly long, but I have cut it down to what I think is a minimal working example:
#!/usr/bin/python3 # -*- coding: utf-8 -*- from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QGridLayout, QWIDGETSIZE_MAX from PyQt5.QtWidgets import QTreeView, QApplication from PyQt5.QtGui import QStandardItemModel, QStandardItem, QFont, QFontMetrics import sys class StartMarshall(QMainWindow): def __init__(self): super().__init__() self.data = ['XXX' for _ in range(8)] # initialize the UI self.initUI() def initUI(self): self.setWindowTitle('Start') # Build Central Widget self.widget = QWidget() self.setCentralWidget(self.widget) # Labels lblInGate = QLabel('In Gate:', self) lblInQueue = QLabel('In Queue:', self) grid = QGridLayout() grid.setSpacing(10) # intialise view of data InGate = QTreeView() self.InQueue = InQueue = QTreeView() # Tried to set colour of whole QTreeView here. #InGate.setColor(QtGui.QColor(255, 100, 0, 255)) fontSize = 12 # Fixed Font font = QFont("monospace",fontSize) font.setStyleHint(QFont.TypeWriter) fontMet = QFontMetrics(font) padd = 4 oneLineHeight = fontMet.lineSpacing() + padd lblInGate.setFont(font) lblInQueue.setFont(font) InGate.setFont(font) InQueue.setFont(font) MinWidth = 500 # set max size of QTree Views InGate.setMaximumSize(QWIDGETSIZE_MAX, oneLineHeight) InQueue.setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX) # set min size of QTree Views InGate.setMinimumSize(MinWidth, oneLineHeight) InQueue.setMinimumSize(MinWidth, oneLineHeight) InQueue.setRootIsDecorated(False) InQueue.setAlternatingRowColors(True) # Setup View Models self.InGateModel = self.prepModel(InGate) self.InQueueModel = self.prepModel(InQueue) # include the widgets grid.addWidget(lblInGate, 2, 0) grid.addWidget(InGate, 2, 1) grid.addWidget(lblInQueue, 3, 0) grid.addWidget(InQueue, 3, 1, -1, -1) self.widget.setLayout(grid) # Show QMainWindow self.show() self.displayRacers() def prepModel(self, widget): # initialize a model model = QStandardItemModel() # remove indentation and headers widget.setIndentation(0) widget.setHeaderHidden(1) # add (data) model to widget widget.setModel(model) return model def fillModel(self, model, data): # for refilling model data for i, d in enumerate(data): model.setItem(i, QStandardItem(d)) #model.setBackgroundColor(QtGui.QColor(255, 100, i, 255)) return def displayRacers(self): self.fillModel(self.InGateModel, self.data[1:2]) # show the full queue (-1 doesnt show last racer?) self.fillModel(self.InQueueModel, self.data[2:len(self.data)]) return # Main if __name__ == '__main__': app = QApplication(sys.argv) ex = StartMarshall() sys.exit(app.exec_())
Advertisement
Answer
To set the whole of a QTreeView Background Colour this works for me:
IG = QTreeView() IG.setStyleSheet("background-color: green");
To set a specific QStandardItemModel Item Background Colour this works for me:
self.IQModel.setData(self.IQModel.index(0, 0), QBrush(QColor(255, 0, 0)), QtCore.Qt.BackgroundRole)
And for completeness, to set the font color, this works for me:
self.InGateModel.setData(self.InQueueModel.index(0, 0), QBrush(Qt.white), QtCore.Qt.ForegroundRole)
Thanks very much to all those that have guided me in finding the answer.
Kevin