PDA

View Full Version : Clear contents of a QTableView cell before repainting



aspidites
21st April 2009, 15:22
First of all, I'm using PyQt4 4.4.3, python 2.6.2, and Qt 4.5.

I am trying to display pixmaps sortable by filename in a QTableView. If I try to pass a QVariant containing a pixmap from my custom model's data method and paint the pixmap in my custom delegate, the images show up perfectly, but sorting isn't possible.

As such, I've decided to instead pass a QVariant containing the file location of the image from my custom model's data method and convert it into a pixmap in the delegate. While sorting now works perfectly, the image is displayed behind the image's location.

I've tried painter.setCompositionMode(), painter.eraseRect(), painter.filRext(), painter.restore(), and painter.setFont() in CustomDelegate to no avail.

Is there some way to either cover the filename with the pixmap, change the fon't of the filename to transparent so that only the pixmap is shown, or clear the contents of the cell before painting the pixmap?

Below is my python code, but I know enough c++ that solutions given in c++ are acceptable. Thanks in advance.

from CustomItemDelegate:


...
class CardItemDelegate(QItemDelegate):
def __init__(self, parent=None):
QItemDelegate.__init__(self, parent)

def paint(self, painter, option, index):
if index.column() == IMAGE:
painter.drawPixmap(option.rect,
QPixmap(index.model().data(index).toString()))
QItemDelegate.paint(self, painter, option, index)

Lykurg
21st April 2009, 15:57
If you paint your pixmap by yourself don't call the default handler. "QItemDelegate.paint(self, painter, option, index)" is the cause why the image name is print over your pixmap. (add an "else")

aspidites
21st April 2009, 16:07
First of all, that was a rediculously fast reply! Second of all, you were right.
I added an else clause and moved the default handler to it and everything works as expeted. Thank you very much!