8

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.

I tried something with a delegate, but I'm not really confortable with them and I couldn't get anything working. I also tried something with the role :

    if index.column() == 4:
        if role == QtCore.Qt.DecorationRole:
            label = QtGui.QLabel()
            path = "path/to/my/picture.jpg"
            image = QtGui.QImage(str(path)) 
            pixmap = QtGui.QPixmap.fromImage(image)
            label.setPixmap(pixmap)
            return label

This piece of code is inspired by something I found in another forum and that was supposed to work. However it doesn't do anything for me, only slows down the execution of my code.

Any idea why it's not working ? If you have an example with a delegate I'd appreciate it also!

Thanks for your attention

RESOLVED: I got it working with a custom delegate. Here is my code if someone's interested :

class ImageDelegate(QtGui.QStyledItemDelegate):

    def __init__(self, parent):
        QtGui.QStyledItemDelegate.__init__(self, parent)

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

        painter.fillRect(option.rect, QtGui.QColor(191,222,185))

        # path = "path\to\my\image.jpg"
        path = "araignee_de_mer.jpg"

        image = QtGui.QImage(str(path))
        pixmap = QtGui.QPixmap.fromImage(image)
        pixmap.scaled(50, 40, QtCore.Qt.KeepAspectRatio)
        painter.drawPixmap(option.rect, pixmap) 
Johanna
  • 1,343
  • 4
  • 25
  • 44

3 Answers3

4

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:

def sizeHint(self, option, index) :
    return QSize(160, 90) # whatever your dimensions are

Swoot!

AteYourLembas
  • 303
  • 3
  • 12
  • if I have multiple path for the image per row, how can I pass the path to the delegate? thanks – unice Jan 24 '14 at 23:59
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)
soneedu
  • 73
  • 1
  • 3
  • 11
0

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
Stephen Terry
  • 6,169
  • 1
  • 28
  • 31
  • 1
    Thanks for your interest, but it doesn't seem to make any difference. I think it calculates the pixmaps because the grid takes a couple of seconds to load and usually it is immediate. But it doesn't display anything – Johanna Jun 27 '11 at 08:06
  • I suspect the problem is with some other part of your code then. Are you sure that you have the correct column? – Stephen Terry Jun 27 '11 at 11:00
  • I finally got it working with a custom delegate. Posting my code in my question – Johanna Jun 27 '11 at 12:02