您似乎有一个XY problem,因为正如您指出的那样,您的目标是“绘制下拉菜单”(这是非常通用的),但您要求可能的解决方案(另一个问题)的错误不会一定要为你服务。
考虑到以上,我会解释错误的原因和可能的解决方案(显然做了很多假设)。
主要错误是这段代码是针对 PyQt4 的,您可能正在使用 PyQt5,在 PyQt4 中,data() 方法返回一个必须转换为 python 对象的 QVariant,因此您使用 isValid() 和 toPyObject() 但是在 pyqt5 中不再需要。另一个错误是 UserRole 值将为 None 因为您在创建项目时没有分配任何值(这可能是由于其他问题中的遗漏造成的)。
考虑到上述情况,一个可能的解决方案(兼容 PyQt4 和 PyQt5)是:
class LineStyleDelegate(QItemDelegate):
def paint(self, painter, option, index):
data = index.data(Qt.UserRole)
if hasattr(data, "toPyObject"):
data = data.toPyObject()
if data is not None:
painter.save()
rect = option.rect
rect.adjust(+5, 0, -5, 0)
pen = QPen()
pen.setColor(Qt.black)
pen.setWidth(3)
pen.setStyle(data)
painter.setPen(pen)
middle = (rect.bottom() + rect.top()) / 2
painter.drawLine(rect.left(), middle, rect.right(), middle)
painter.restore()
else:
QItemDelegate.paint(self, painter, option, index)
self.searchEdit = QComboBox(sef.searchContent)
for text, style in (
("Item 1", Qt.SolidLine),
("Item 2", Qt.DotLine),
("Item 3", Qt.DashDotDotLine),
):
self.searchEdit.addItem(text, style)
self.delegate = LineStyleDelegate(self.searchEdit)
self.searchEdit.setItemDelegate(self.delegate)
self.searchEdit.setMinimumWidth(500)
self.searchEdit.setEditable(True)
更新:
通过修改问题,可以验证 OP 存在 XY 问题。要修改 QComboBox 的项目的绘制,必须使用委托:QItemDelegate 或 QStyledItemDelegate。我更喜欢使用第二种,因为它使用的是QStyle,即设计会尊重GUI的风格。要设置每个项目的颜色,请使用 QStyleOptionViewItem 的 backgroundBrush 属性,并且对于边框绘制,则必须重写 paint() 方法:
import random
from PyQt5 import QtCore, QtGui, QtWidgets
class CustomStyleDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(CustomStyleDelegate, self).initStyleOption(option, index)
random_color = QtGui.QColor(*random.sample(range(255), 3))
option.backgroundBrush = random_color
def paint(self, painter, option, index):
super(CustomStyleDelegate, self).paint(painter, option, index)
margins = 2
border_color = QtGui.QColor(*random.sample(range(255), 3))
painter.save()
pen = QtGui.QPen()
pen.setColor(border_color)
pen.setWidth(margins)
painter.setPen(pen)
r = QtCore.QRect(option.rect).adjusted(0, 0, -margins, -margins)
painter.drawRect(r)
painter.restore()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QComboBox()
w.addItems(["Item 1", "Item 2", "Item 3"])
delegate = CustomStyleDelegate(w)
w.setItemDelegate(delegate)
w.show()
sys.exit(app.exec_())