我正在尝试为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_())
答案 0 :(得分:0)
要设置整个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)
非常感谢所有引导我找到答案的人。
凯文