Python PyQt5 QTreeView set row Background Colour



我正在尝试为a(整个QTreeView和b(在Python中的QTreeView中的特定行设置背景颜色(颜色(。

我找到了setColor和setBackgroundColor方法,但似乎都不适合我使用QTreeView或QStandardItem。

很多谷歌搜索显示了很多关于它的对话,但我无法将这些与下面的代码联系起来。

下面是完整代码,但设置颜色的两次尝试是:

    InGate = QTreeView()
    InGate.setColor(QtGui.QColor(255, 100, 0, 255))

        for i, d in enumerate(data):
            model.setItem(i, QStandardItem(d))
            model.setBackgroundColor(QtGui.QColor(255, 100, i, 255))

任何帮助表示赞赏。

非常感谢凯文

抱歉,代码示例相当长,但我已将其缩减为我认为是最小工作示例的内容:

#!/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_())

要设置整个 QTreeView 背景颜色,这对我有用:

    IG = QTreeView()
    IG.setStyleSheet("background-color: green");

要设置特定的QStandardItem模型项目背景颜色,这对我有用:

    self.IQModel.setData(self.IQModel.index(0, 0), QBrush(QColor(255, 0, 0)), QtCore.Qt.BackgroundRole)
为了

完整起见,为了设置字体颜色,这对我有用:

    self.InGateModel.setData(self.InQueueModel.index(0, 0), QBrush(Qt.white), QtCore.Qt.ForegroundRole)

非常感谢所有指导我找到答案的人。

凯文

最新更新