簡體   English   中英

Python PyQt5 QTreeView 設置行背景顏色

[英]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");

要設置特定的QStandardItemModel項背景色,這對我有效:

    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)

非常感謝所有引導我尋找答案的人。

凱文

@Kevin W 很抱歉寫這個作為答案,但它不允許我發送評論。 您的解決方案中的IQModel是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM