【发布时间】:2019-09-04 15:06:10
【问题描述】:
我正在尝试为 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_())
【问题讨论】:
-
BackgroundRole是您所需要的。为特定项目设置背景:standard_item.setData(color, QtCore.Qt.BackgroundRole). -
感谢@KingStone 的链接,但这对我来说没有任何意义。该链接上有多个答案,我无法将它们与我的代码相关联?
-
感谢@Avaris,但我如何引用我的 QStandardItemModel 的特定行分配给我的代码中的特定 QTreeView() ?在 prepModel 或 fillModel 方法中? doc.qt.io/archives/qt-5.10/qstandarditemmodel.html#setData 真的没有帮助我:(
-
@KevinW 有多种方法可以做到这一点。在
fillModel,model.setItem(i, QStandardItem(d))-> 先存储项目,设置背景,然后setItem它。或者稍后针对特定项目执行此操作model.item(row, column).setData(...)。
标签: python python-3.x pyqt pyqt5