View Full Version : QTableView with a column of images

24th June 2011, 10:20
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)
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

Santosh Reddy
25th June 2011, 11:47
try returning pixmap, an also make sure that image is available in the path

27th June 2011, 10:41
I also tried returning a pixmap with no result. But maybe it is not an issue with the pixmap. I just tried to return a color

return QtCore.QVariant(QtGui.QBrush(QtGui.QColor(QtCore.Q t.red)))

And it didn't change the color of the cells. However it prints something if I do
print "test" in the condition block.

Any idea ?

Santosh Reddy
27th June 2011, 10:53
You should be supplying the images / pixmap / color from the view's model, and not from the delegate

27th June 2011, 10:55
Have you tried Qt::BackgroundRole instead of Qt::DecorationRole?

27th June 2011, 11:09
@Rachol : Indeed it makes sense that it did not work with the color ! It works fine with the backgroundRole for the color, so it is a probleme with the pixmap.

@Santosh : I'm supplying it from my model, I don't have a custom delegate

Santosh Reddy
27th June 2011, 11:24
post you undated code which returns pixmap..

27th June 2011, 11:27
For BackgroundRole you must always return QBrush. You can use void QBrush::setTexture ( const QPixmap & pixmap ) to set the image use to draw your background. But then it must be a correct size, so it is not drawn couple of times.

The only reasonable solution would be to subclass QStyledItemDelagate (not QItemDelegate) and reimplement paint method. If you have painted a QWidget before, this should be very straight forward.

Use setItemDelegateForColumn method to set a delegate on your TableView

27th June 2011, 11:45
I'm trying something with a delegate but so far I got nothing, maybe you can help me to get this straight.
I created a custom delegate inheriting QStyledItemDelegate :

class ImageDelegate(QtGui.QStyledItemDelegate):

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

def paint(self, painter, option, index):
# Get Item Data

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

style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionGraphicsItem()
opt.rect = option.rect
pixmap = QtGui.QPixmap(path)

painter.drawPixmap(opt.rect, pixmap)

And I call it like this :

self.delegateImage = ImageDelegate(self)
self.setItemDelegateForColumn(2, self.delegateImage)

And I am sure the path is correct

What am I missing ?

27th June 2011, 12:08
Okay, I am not a specialist on python, couldn't even call myself a beginner. However I think you don't need this:

style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionGraphicsItem()
opt.rect = option.rect

Just use option.rect directly.

Appart from that there is nothing I could say is wrong or unnecessary. Have you tried to fill option.rect with some color?

27th June 2011, 12:30
Filling the cells with a color works fine :

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

But still no changes with the pixmap

27th June 2011, 12:59
The there are only 2 things can go wrong:

1. Your pixmap is not loaded(wrong path)
2. Your pixmap is not scalled to the size of the area you are drawing into.

27th June 2011, 14:26
Well I got it working.
You were right, there was a problem in my path. I tried to rescale the pixmap and only then it said that it was null. So I put an image in my code folder and it displays it. The thing is that the path is correct, but it couldn't access it. Maybe it's due to the fact that's on another drive. And that's a different problem !

Thank you for your time !

I'm just posting my working piece of code as a reminder :

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 = "image.jpg"

image = QtGui.QImage(str(path))
pixmap = QtGui.QPixmap.fromImage(image)
pixmap.scaled(50, 40, QtCore.Qt.KeepAspectRatio)
painter.drawPixmap(option.rect, pixmap)

Added after 21 minutes:

About the path : I just needed to double the backslashes, everything's perfect now :)

7th February 2012, 13:25
We are working with the Browser developed using 4.5 and we are trying to read XML files from the internet and trying to show the images that comes in the RSS feeds along with the title and link to the article.

Except for the image all the data is visible on the label in the QT Window Application form. Even the console window of MSVC2008 also shows no error.

Searched the net for answers and could not find any.

Any help in this regard would be highly appreciated.

Sunil M