[PyKDE] autoAdding buttons to a grid

Richard Smith smithbone at gmail.com
Fri May 20 11:34:45 BST 2005


On 5/19/05, Richard Smith <smithbone at gmail.com> wrote:
> >
> > Here's a crude example that may do something like what you were
> > originally trying to do:
> 
> I tried to use your same QGrid model with a QButtonGroup but there is
> still a piece of the puzzle I'm missing as just creating a

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.

=============

import sys
from qt import *

class Form(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.columns = 2
        self.bgMain = QButtonGroup(self)
        self.bgMain.setColumnLayout(self.columns,Qt.Horizontal)
        self.bgMain.layout().setMargin(5)        
        b = QPushButton("Add Button",self.bgMain)
        self.connect(b,SIGNAL('clicked()'),self.handler)
        b = QPushButton("Reflow",self.bgMain)
        self.connect(b,SIGNAL('clicked()'),self.col_reflow)
        self.count = 2
        
        for n in range(self.count,6):
            QPushButton("Button %d" % n, self.bgMain)
            self.count += 1
        
        self.rows = self.count / self.columns
        r = QRect(10,10,200,self.rows*(b.height()+self.bgMain.layout().spacing()))
        self.bgMain.setGeometry(r)

#        self.bgMain.setGeometry(QRect(10,10,200,3*b.height()))
        self.resize(QSize(300,500))
        self.count = 5
    def handler(self):
        self.count += 1
        b = QPushButton("Button %d" % self.count,self.bgMain)
        b.show()
        self.rows = (self.count / self.columns) + 1
        self.bgMain.resize(200,self.rows*(b.height()+self.bgMain.layout().spacing()))

    def col_reflow(self):
        self.columns +=1
        self.bgMain.setColumns(self.columns)
        
if __name__ == "__main__":
   A = QApplication(sys.argv)
   F = Form(); A.setMainWidget(F); F.show()
   A.exec_loop(); del F



-- 
Richard A. Smith




More information about the PyQt mailing list