在我的示例代码中,我想为树小部件中的每一行的父文本应用不同的颜色,如何将背景颜色应用于父文本行,我尝试了Qbrush,但没想到output.soc可以告诉我一个人吗?预先谢谢你。
下面是我的示例代码:
from PyQt4 import QtCore, QtGui
class InventoryDelegate(QtGui.QStyledItemDelegate):
def paint(self, painter, option, index):
super(InventoryDelegate, self).paint(painter, option, index)
if not index.parent().isValid():
painter.save()
painter.setPen(QtGui.QPen(QtGui.QColor("green")))
r = QtCore.QRect(option.rect)
r.adjust(0, 1, 0, -1)
painter.drawLine(r.topLeft(), r.topRight())
painter.drawLine(r.bottomLeft(), r.bottomRight())
painter.restore()
def sizeHint(self, option, index):
s = super(InventoryDelegate, self).sizeHint(option, index)
s.setHeight(55)
return s
class Inventory_Items(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Inventory_Items, self).__init__(parent)
self.inventory_dict = {
"Shirt": {
"Formal Blue shirt": ["10.10.1(Red)", "10.10.2(blue)"],
"Discount:0%": ["300", "400"],
"Unit:Quantity": ["10", "5"]
},
"Frocks": {
"Partywear Frock": ["10.10.1(pink)", "10.10.1(black)"],
"Discount:0%": ["900", "1200"],
"Unit:Quantity": ["50", "30"]
}
}
self.inventory_widget = QtGui.QTreeWidget(columnCount=7,
expandsOnDoubleClick=False,
indentation=0,
focusPolicy=QtCore.Qt.NoFocus,
iconSize=QtCore.QSize(40,60))
self.inventory_widget.setColumnWidth(2, 350)
self.inventory_widget.setStyleSheet("QWidget {background-color: black;color: white;}")
header = self.inventory_widget.header()
header.hide()
delegate = InventoryDelegate(self.inventory_widget)
self.inventory_widget.setItemDelegate(delegate)
for key, value in self.inventory_dict.items():
icon = QtGui.QIcon("./{}.png".format(key))
it = QtGui.QTreeWidgetItem()
self.inventory_widget.addTopLevelItem(it)
it.setIcon(0, icon)
k = list(value.keys())
for i, t in enumerate([key] + k):
it.setText(i+1, t)
e = [[] for _ in value[k[0]]]
for k, v in value.items():
for i, val in enumerate(v):
e[i].append(val)
for r in e:
child = QtGui.QTreeWidgetItem()
for i, e_ in enumerate(r):
child.setText(i+2, e_)
it.addChild(child)
self.inventory_widget.expandAll()
self.setCentralWidget(self.inventory_widget)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
settingobj= Inventory_Items()
settingobj.showFullScreen()
sys.exit(app.exec_())
答案 0 :(得分:2)
假设您要更改顶级行的颜色,则必须覆盖initStyleOption()
方法:
class InventoryDelegate(QtGui.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(InventoryDelegate, self).initStyleOption(option, index)
if not index.parent().isValid():
option.backgroundBrush = QtGui.QBrush(QtGui.QColor("gray"))
def paint(self, painter, option, index):
super(InventoryDelegate, self).paint(painter, option, index)
if not index.parent().isValid():
painter.save()
painter.setPen(QtGui.QPen(QtGui.QColor("green")))
r = QtCore.QRect(option.rect)
r.adjust(0, 1, 0, -1)
painter.drawLine(r.topLeft(), r.topRight())
painter.drawLine(r.bottomLeft(), r.bottomRight())
painter.restore()
def sizeHint(self, option, index):
s = super(InventoryDelegate, self).sizeHint(option, index)
s.setHeight(55)
return s