[PyKDE] Painting one cell of a table

Jim Bublitz jbublitz at nwinternet.com
Sun Mar 6 19:01:45 GMT 2005


On Saturday 05 March 2005 18:17, Jorge Godoy wrote:
> Hi,
>
>
> I'm trying to paint one cell in a table but the result is put on the wrong
> place.  My code is:
>
>
> 		row = 2
> 		col = 2
>
> 		cg = QColorGroup()
> 		cg.setColor(QColorGroup.Base, Qt.red)
>
> 		cr = self.table1.cellGeometry(row, col)
> 		#cr = self.table1.cellRect(row, col)
>
> 		print cr.x(), cr.y()
>
> 		painter = QPainter(self.table1)
>
> 		self.table1.paintCell(painter, row, col, cr, False, cg)
>
>
> If I use cellGeometry the print statement shows "200 40".  If I use
> cellRect it shows "0 0".  It doesn't matter with which one, the red cell
> appears at position 0, 0 and the table is drawn over it.  How can I make
> the red cell appears at the row and col given?
>
> I have already tried with QTableItem and the result is the same:
>
>         currentItem = self.table1.item(row, col)
> 		currentItem.paint(painter, cg, cr, False)

Pete provided the QTable version - here's one way to do it with a QTableItem 
subclass. You could also modify paint to automatically set colors based on 
some criteria from the cell value (eg red if < 0, black if >= 0).

class ColorTableItem (QTableItem):
    def __init__ (self, table, text):
        self.bgColor = None
        self.fgColor = None
        QTableItem.__init__ (self, table, QTableItem.OnTyping, text)

    def setColor (self, bgColor, fgColor):
        self.bgColor = bgColor
        self.fgColor = fgColor

    def paint (self, painter, colorgrp, rect, selected):
        if self.fgColor:
            colorgrp.setColor (QColorGroup.Text, self.fgColor)

        if self.bgColor:
            colorgrp.setColor (QColorGroup.Base, self.bgColor)

        QTableItem.paint (self, painter, colorgrp, rect, selected)





More information about the PyQt mailing list