gpt4 book ai didi

python - 为 QListWidget 中的特定项目设置不同的颜色

转载 作者:太空狗 更新时间:2023-10-30 02:56:17 70 4
gpt4 key购买 nike

我有一个 QListWidget,我想为列表的每个项目添加边框底部并为项目设置背景颜色,我想为特定项目设置不同的背景颜色。所以我使用了 my_list.setStyleSheet("QListWidget::item { border-bottom: 1px solid red; background-color: blue;}") 并将背景颜色设置为我使用的特定项目 item.setBackground(QtGui.QColor("#E3E3E3"))

问题是我设置了不同颜色的特定项目没有得到这种颜色。

最佳答案

您不能在小部件上使用样式表和样式设置的组合 - 样式表将覆盖单个项目的任何设置。例如,以下代码对每个项目使用 setBackground 来更改背景颜色。

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

colors = [ '#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666']

class MainWindow(QMainWindow):

def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)

w = QListWidget()
for n in range(8):
i = QListWidgetItem('%s' % n)
i.setBackground( QColor(colors[n]) )
w.addItem(i)

self.setCentralWidget(w)

self.show()


app = QApplication([])
w = MainWindow()
app.exec_()

结果输出:

enter image description here

但是,如果我们在结果中添加样式表行(第二个只有底部边框):

enter image description here enter image description here

不幸的是,无法设置项目的边框和颜色。但是,您可以做的是插入自定义小部件作为列表项,或者 use an item delegate绘制项目。这使您可以完全控制外观,但是您必须自己处理绘图。下面是使用自定义委托(delegate)执行此操作的示例:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

colors = [ '#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666']


class MyDelegate(QItemDelegate):
def __init__(self, parent=None, *args):
QItemDelegate.__init__(self, parent, *args)

def paint(self, painter, option, index):
painter.save()

# set background color
painter.setPen(QPen(Qt.NoPen))
if option.state & QStyle.State_Selected:
# If the item is selected, always draw background red
painter.setBrush(QBrush(Qt.red))
else:
c = index.data(Qt.DisplayRole+1) # Get the color
painter.setBrush(QBrush(QColor(c)))

# Draw the background rectangle
painter.drawRect(option.rect)

# Draw the bottom border
# option.rect is the shape of the item; top left bottom right
# e.g. 0, 0, 256, 16 in the parent listwidget
painter.setPen(QPen(Qt.red))
painter.drawLine(option.rect.bottomLeft(), option.rect.bottomRight())

# Draw the text
painter.setPen(QPen(Qt.black))
text = index.data(Qt.DisplayRole)
# Adjust the rect (to pad)
option.rect.setLeft(5)
option.rect.setRight(option.rect.right()-5)
painter.drawText(option.rect, Qt.AlignLeft, text)

painter.restore()


class MainWindow(QMainWindow):

def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)

de = MyDelegate(self)
w = QListWidget()
w.setItemDelegate(de)
for n in range(8):
s = '%s' % n
i = QListWidgetItem()
i.setData(Qt.DisplayRole, s) # The label
i.setData(Qt.DisplayRole+1, colors[n]) # The color
w.addItem(i)

self.setCentralWidget(w)

self.show()


app = QApplication([])
w = MainWindow()
app.exec_()

给出以下输出:

enter image description here

委托(delegate)的方法是定义一个 paint 方法,它接受一个 QPainter 对象(用它来进行实际绘图),一个 option 参数包含项目的矩形(相对于父小部件)和一个 index ,您可以通过它检索项目数据。然后您使用 QPainter 上的方法绘制你的项目。

在上面的示例中,我们使用它来传递项目标签(在 Qt.DisplayRole 位置)和十六进制颜色(在 Qt.DisplayRole+1 位置) >). ItemDisplayRole 的名称文档列出其他定义的数据“角色”,但对于大多数用途,您可以忽略这些。

关于python - 为 QListWidget 中的特定项目设置不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39995688/

70 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com