[PyKDE] QTable and background color.

David Sathiaraj davids at srcc.lsu.edu
Wed Oct 15 23:42:01 BST 2003


Gianluca:
I scripted out a small code snippet to do what you were trying and I am 
getting consitent results. 
You can use the attached code to check if you are doing something different.
Regards,
David

#!/usr/bin/python
import sys
from qt import *
from qttable import *

class MyTableItem(QTableItem):
         def __init__(self, table, edittype, text):
                 QTableItem.__init__(self, table, edittype, text)

         def paint(self, painter, colorgroup, rect, selected):
                 cg = QColorGroup(colorgroup)
                 cg.setColor(QColorGroup.Base, Qt.red)
                 QTableItem.paint(self,painter, cg, rect, selected)

class MyTable(QTable):
         def __init__(self, parent=None, name="MyTable"):
                 QTable.__init__(self, 10, 4, parent, name)
                 self.tableitem = MyTableItem(self,QTableItem.Never,"Hello World")
                 for i in xrange(3):
                        for j in xrange(2):
                                self.tableitem.setText("Hello again")
                                self.setItem(i, j, self.tableitem)
                                self.updateCell(i,j)

if __name__=="__main__":
         app = QApplication(sys.argv)
         mw = MyTable()
         app.setMainWidget(mw)
         mw.show()
         app.exec_loop()






Gianluca wrote:

>Hello.
>
>I am trying to implement a QTable where n cells on every row are painted on a 
>different color.
>
>Searching on the ML, I find that I must reimplement the paint method. Fine.
>Now I have only one problem: the setting of the color is located in a for 
>cicle (I don't know in advance what cell I must modify), but I only get a red 
>background on the last cell I change.
>
>This is the code i use:
>
>for x in range(0,cols):
>	self.item=colorItem(self.tabella,QTableItem.Never,self.tabella.text(barca,x))
>	self.item.setText(self.tabella.text(barca,x))
>	self.tabella.setItem(barca, x, self.item)
>	self.tabella.updateCell(barca,x)
>
>(the for over the rows are external to this one)
>
>and this is the implementation of the paint method:
>
>
>class colorItem(QTableItem):
>	def __init__(self, table, edittype, text):
>		QTableItem.__init__(self,table,edittype,text)
>
>
>	def paint(self, painter, colorgroup, rect, selected):
>		cg = QColorGroup(colorgroup)
>		cg.setColor(QColorGroup.Base, Qt.red)
>		QTableItem.paint(self,painter, cg, rect, selected)
>
>
>
>It works, but only for the last cell I update
>
>example:
>
>QTable 6*10
>I need to change 2 cell on every row.
>All the cells I need to change lose the text, except for the last one, who 
>mantain the correct text and have a red background.
>
>
>Any hints ?
>
>thanks.
>
>Gianluca
>
>
>  
>


-- 
------------------------------------------------------
David Sathiaraj
Applications Programmer
Southern Regional Climate Center
E328 Howe Russel,
Lousiana State University,
Baton Rouge, LA 70803
------------------------------------------------------





More information about the PyQt mailing list