[PyKDE] Aligning Text in a Table
Bob Parnes
rparnes at megalink.net
Sun Jun 29 01:05:00 BST 2003
On Sat, Jun 28, 2003 at 03:10:40PM +0200, Simon Edwards wrote:
> Hi,
>
> > Thanks for your response. I got lost trying to follow your idea. In the
> > past I have subclassed my own classes and reimplemented methods in order
> > to make changes suiting the subclass. But here I cannot even make the
> > simplest reimplementation of paintCell. For example, the method,
> >
> > def paintCell(self,p,row,col,r,selected):
> > QTable.paintCell(self,p,row,col,r,selected)
>
> Does this work?:
>
> def paintCell(self,p,row,col,r,selected,cg):
> QTable.paintCell(self,p,row,col,r,selected,cg)
>
> Maybe PyQt doesn't have the 6 arg version (it's depreciated BTW),
Yes, it does work, although I used Phil Thompson's version.
>
> > Furthermore, I don't know how to identify the QPainter object that Qt
> > passes. I could not find any place in the documentation that describes
> > how to do this. In my attempt above, I created one.
>
> errrr... You don't need to identify anything. YOu just draw with the QPainter
> that you get.
>
> cheers,
>
> --
> Simon Edwards | Guarddog Firewall
> simon at simonzone.com | http://www.simonzone.com/software/
> Nijmegen, The Netherlands | "ZooTV? You made the right choice."
>
I did not read your post carefully enough and thought that I had to make
the call to paintCell. However, it is confusing, because now I do not
understand the purpose of the arguments beyond the row and column.
Anyway, I did manage to successfully draw an example, so long as the
code was in the paintCell implementation. But I could not write
to the table at a later time. The attached code, where the pushbutton is
supposed to display the data, shows my attempt. Qt returns a new
QPainter object, but it doesn't draw. So I am still missing something.
I really appreciate your help, and I hope I can see this through
successfully. It has been an education for me.
--
Bob Parnes
rparnes at megalink.net
-------------- next part --------------
#!/usr/bin/env python
import sys
from qt import *
from qttable import QTable
class newTable(QTable):
def __init(self,parent,title):
QTable.__init__(self,parent,title)
def drawText(self):
for row in range(3):
for col in range(3):
r = self.cellRect(row,col)
if row ==1 and col == 1: print 'p,r at (1,1):',self.p,r
if col == 1:
self.p.drawText(r,Qt.AlignRight,'text%d%d' % (row,col))
else:
self.p.drawText(r,Qt.AlignLeft,'text%d%d' % (row,col))
def paintCell(self,p,row,col,r,selected,cg=None):
self.p = p
if cg:
QTable.paintCell(self,p,row,col,r,selected,cg)
else:
QTable.paintCell(self,p,row,col,r,selected)
# r = self.cellRect(row,col)
# if col == 1:
# p.drawText(r,Qt.AlignRight,'text%d%d' % (row,col))
# else:
# p.drawText(r,Qt.AlignLeft,'text%d%d' % (row,col))
class Form1(QWidget):
def __init__(self,parent = None,name = None,fl = 0):
QWidget.__init__(self,parent,name,fl)
if not name:
self.setName("Form1")
self.table = newTable(self,"table")
self.table.setGeometry(QRect(50,60,335,104))
self.table.setNumRows(3)
self.table.setNumCols(3)
self.pb = QPushButton(self,'getData')
self.connect(self.pb,SIGNAL('clicked()'),self.getData)
self.languageChange()
self.resize(QSize(458,258).expandedTo(self.minimumSizeHint()))
def getData(self):
self.table.drawText()
def languageChange(self):
self.setCaption(self.tr("Alignment Test"))
self.pb.setText(self.tr("Display Data"))
if __name__ == "__main__":
a = QApplication(sys.argv)
QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
w = Form1()
a.setMainWidget(w)
w.show()
a.exec_loop()
More information about the PyQt
mailing list