[PyQt] TableView Cell Delegates
David Boddie
david at boddie.org.uk
Mon Oct 29 21:25:42 GMT 2012
On Mon, 29 Oct 2012 11:09:25 +0000, Cristobal Infante wrote:
> What I want is to change the background of a tableview cell, from what I
> understand
> I should be looking at the Qt.BackgroundRole. So far I havent bee able to
> get it working.
You need to make sure that the model supplies data for the BackgroundRole
of each item as well as for the standard DisplayRole. The standard styled
delegate will then make sure that the cell uses that background colour.
> What I have so far, is a delegate that fills the cell with a color, but it
> seems to be going
> on top of the text:
>
> class CellBackgroundColor(QtGui.QStyledItemDelegate):
>
> def __init__(self, parent = None):
> QtGui.QStyledItemDelegate.__init__(self, parent)
>
> def paint(self, painter, option, index):
>
> path = index.model().data(index, QtCore.Qt.DisplayRole).toString()
> painter.fillRect(option.rect, QtGui.QColor(path))
>
> Any ideas on how to implement this Qt.BackgroundRole in a tableview
> delegate?
If the model does not supply data for the BackgroundRole, you can use a
custom delegate to paint the background in the way you have shown, but you
then need to call the base class's implementation of paint().
def paint(self, painter, option, index):
path = index.model().data(index, QtCore.Qt.DisplayRole).toString()
painter.fillRect(option.rect, QtGui.QColor(path))
QStyledItemDelegate.paint(self, painter, option, index)
Good luck!
David
More information about the PyQt
mailing list