Qtableview With A Column Of Images
I have a QTableView to display some informations of a database in the form of a grid. One of the fields is a path to an image and I would like to display these images in my table.
Solution 1:
Thank you for the solution and the code example, Johanna. It was a huge help.
In addition, in case anyone else needs it spelled out like I did:
1) Set the item delegate for the image-bearing column of your QTableView (I did this in myTableView.init()) like so:
self.setItemDelegateForColumn(1, yourImageDelegate(parent))
2) In yourImageDelegate class, you may want to overload sizeHint() for the size of your image like so:
defsizeHint(self, option, index) :
return QSize(160, 90) # whatever your dimensions are
Swoot!
Solution 2:
when i test , i use drawPixmap(option.rect.x(), option.rect.y(), pixmap) to draw icon, noneed do pixmap.scaled.
class ImageDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent=None):
QtGui.QStyledItemDelegate.__init__(self, parent)
#self.icon =icon
def paint(self, painter, option, index):
#painter.fillRect(option.rect, QtGui.QColor(191,222,185))
# path = "path\to\my\image.jpg"
path = "icon1.png"
image = QtGui.QImage(str(path))
pixmap = QtGui.QPixmap.fromImage(image)
#pixmap.scaled(16, 16, QtCore.Qt.KeepAspectRatio)
# when i test ,just use option.rect.x(), option.rect.y(), no need scaled
painter.drawPixmap(option.rect.x(), option.rect.y(), pixmap)
Solution 3:
I don't think you need to encapsulate the Pixmap in a Label or an Image. Try just returning the Pixmap.
if index.column() == 4:
if role == QtCore.Qt.DecorationRole:
path = "path/to/my/picture.jpg"
pixmap = QtGui.QPixmap(path)
return pixmap
Post a Comment for "Qtableview With A Column Of Images"