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.
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 :
Code:
if index.column() == 4:
if role == QtCore.Qt.DecorationRole:
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
Re: QTableView with a column of images
try returning pixmap, an also make sure that image is available in the path
Re: QTableView with a column of images
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
And it didn't change the color of the cells. However it prints something if I do in the condition block.
Any idea ?
Re: QTableView with a column of images
You should be supplying the images / pixmap / color from the view's model, and not from the delegate
Re: QTableView with a column of images
Have you tried Qt::BackgroundRole instead of Qt::DecorationRole?
Re: QTableView with a column of images
@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
Re: QTableView with a column of images
post you undated code which returns pixmap..
Re: QTableView with a column of images
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
Re: QTableView with a column of images
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 :
Code:
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"
opt.rect = option.rect
painter.drawPixmap(opt.rect, pixmap)
And I call it like this :
Code:
self.delegateImage = ImageDelegate(self)
self.setItemDelegateForColumn(2, self.delegateImage)
And I am sure the path is correct
What am I missing ?
Re: QTableView with a column of images
Okay, I am not a specialist on python, couldn't even call myself a beginner. However I think you don't need this:
Code:
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?
Re: QTableView with a column of images
Filling the cells with a color works fine :
Code:
painter.
fillRect(option.
rect, QtGui.
QColor(191,
222,
185))
But still no changes with the pixmap
Re: QTableView with a column of images
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.
Re: QTableView with a column of images
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 :
Code:
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 :)
Re: QTableView with a column of images
Hi,
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.
Thanks
Sunil M