[PyKDE] autoAdding buttons to a grid

John Ridley ojokimu at yahoo.co.uk
Fri May 20 23:13:55 BST 2005


--- Richard Smith <smithbone at gmail.com> wrote:
> I must be finally starting to catch on.  I worked out how to make the
> QbuttonGroup operate pretty close to what I want.
> 
> The code I ended up with is below.
> 
> One thing I this still dosen't do is resize all the buttons when the
> number of columns is changed.  I want the button widths to shrink
> with
> repect to how wide I set the
> QButtonGroup to be.
> 
> QGridLayout seems to be what I want to use to do all this for me but
> I
> haven't yet fully grokked how to make that happen.

Is this anything like what you had in mind?

>>> import sys
>>> from qt import *
>>> class Form(QDialog):
...    def __init__(self):
...       QDialog.__init__(self)
...       self.grid = QGridLayout(None,1,1,0,6)
...       p = QSizePolicy.Expanding; p = QSizePolicy(p,p,0,0,1)
...       b = QPushButton('Add Row',self); b.setSizePolicy(p)
...       self.connect(b,SIGNAL('clicked()'),self.addbuttons)
...       self.grid.addWidget(b,0,0)
...       b = QPushButton('Add Col',self); b.setSizePolicy(p)
...       self.connect(b,SIGNAL('clicked()'),self.addcol)
...       self.grid.addWidget(b,1,0)
...       g = QGridLayout(self,1,1,10,6)
...       g.addLayout(self.grid,0,0)
...       self.resize(QSize(300,300))
...    def addbuttons(self, col=0):
...       k = [self.grid.numRows(),self.grid.numCols()]
...       if col: k.reverse()
...       for n in range(k[1]):
...          b = QPushButton('Button', self)
...          b.setSizePolicy(QSizePolicy(
...             QSizePolicy.Expanding,QSizePolicy.Expanding,0,0,1))
...          if col: self.grid.addWidget(b,n,k[0])
...          else: self.grid.addWidget(b,k[0],n)
...          b.show()
...    def addcol(self):
...       self.addbuttons(1)
...
>>> A = QApplication(sys.argv)
>>> F = Form(); A.setMainWidget(F); F.show()
>>> A.exec_loop(); del F,A

HTH

John Ridley


		
___________________________________________________________ 
Does your mail provider give you FREE antivirus protection? 
Get Yahoo! Mail http://uk.mail.yahoo.com




More information about the PyQt mailing list