我玩QStyledItemDelegate 只是为了好玩和学习,如何将文本和其他元素放置在委托的项目中。
您可以为每个项目向模型 (UserRole) 添加图标列表。子类 QStyledItemDelegate 并覆盖 paint() 以像这样由委托绘制图标
这是一个随机设置图标的工作示例:
import sys, random
from PyQt5 import QtGui, QtCore, QtWidgets
class ViewDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self):
QtWidgets.QStyledItemDelegate.__init__(self)
self.padding = 2
self.AlignmentFlag = QtCore.Qt.AlignRight
def paint(self, painter, option, index):
x = option.rect.x()
y = option.rect.y()
width = option.rect.width()
height = option.rect.height()
iconList = index.data(256) # get the icons associated with the item
text = index.data() # get the items text
for a, i in enumerate(iconList):
m = max([i.width(), i.height()])
f = (height- 2*self.padding)/m # scalingfactor
i = i.scaled(int(i.width()*f),int(i.height()*f)) # scale all pixmaps to the same size depending on lineheight
painter.drawPixmap(QtCore.QPoint(x,y+self.padding),i)
x += height
painter.drawText(QtCore.QRect(x + self.padding,y + self.padding, width - x - 2*self.padding, height - 2*self.padding),self.AlignmentFlag, text)
class MyWidget(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setGeometry(200,100,200,220)
self.icons = []
for i in ('arrow_double.svg', 'asc.png', 'desc.png', 'checked.png'):
icon = QtGui.QPixmap(i)
self.icons.append(icon)
self.listWidget = QtWidgets.QListView(self)
self.delegate = ViewDelegate()
self.listWidget.setGeometry(20,20,160,180)
self.listWidget.setItemDelegate(self.delegate)
self.model = QtGui.QStandardItemModel(self.listWidget)
itemsTexts = ['abcde','fghij','klmno','pqrst','uvwxy','zABCD']
for r, i in enumerate(itemsTexts):
item = QtGui.QStandardItem(i)
self.model.setItem(r,0, item)
self.model.setData(self.model.index(r,0), self.iconList(), 256) # save the items icons in userRole
self.listWidget.setModel(self.model)
def iconList(self): # creates a random iconList
r = random.randint(1,len(self.icons)) # Anzahl icons in the list
iconList = []
for i in range(0,r):
n = random.randint(0,len(self.icons)-1)
icon = self.icons[n]
iconList.append(icon)
return iconList
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())