With PyQt5 I am trying to use QItemDelegate to show an icon instead of a text string in a cell in a table. Essentially I construct a subclass of the QItemDelegate using:
de = MyDelegate(self.attribute_table_view)
Here dself.attribute_table_view
is a `QTableView' object.
I try to draw an icon in every cell in a specific column using:
class MyDelegate(QItemDelegate):
def __init__(self, parent=None, *args):
QItemDelegate.__init__(self, parent, *args)
def paint(self, painter, option, index):
painter.save()
value = index.data(Qt.DisplayRole)
line_1x = QPixmap('line_1x.png')
painter.setBrush(Qt.gray)
painter.setPen(Qt.black)
painter.drawPixmap(QRectF(0, 0, 48, 24), line_1x, QRectF(0, 0, 48, 24))
painter.restore()
With the painter.drawPixmap()
how do I tell it to draw in each cell in the table like one achieves using painter.drawText(option.rect, Qt.AlignVCenter, value)
?
Also, I have noticed that my current script does not report any errors if I enter a filename that doesn't exist for the .png file. Should an error by reported if the .png file does not exist?
My current model is a QgsAttributeTableModel and I want to render the current string value for all cells in one column with icon's where the icon used depends on the string value.