[PyKDE] autoAdding buttons to a grid
Phil Thompson
phil at riverbankcomputing.co.uk
Tue May 17 15:42:49 BST 2005
> I just started using pyQt and I seem to need a little guidance...
>
> I'm trying to auto add several more QPushButtons to a QGridLayout but
> I either get runtime type errors or if it dosen't error then they
> don't seem to show.
>
> I have a form with a QButtonGroup on it that I created with the
> designer called bgMain. I've subclassed my window from the class
> generated by pyuic
>
> Via the designer I have 3 buttons in the buttongroup and they are
> managed by a GridLayout
>
> So I have self.bgMain which is the reference to the buttongroup.
>
> When one of the buttons is clicked I want to populate several more
> buttons into that gridlayout in the buttongroup. I don't really care
> about the order so AutoAdd seemed to be what I needed.
>
> Looking at the pyuic code and reading the Qt docs I've come up with
>
> items = ["test1","test2","test3"]
> bgMainLayout = QGridLayout(self.bgMain.layout())
> bgMainLayout.setAutoAdd(1)
>
> for each in items:
> self.bgMain.insert(QPushButton(self.bgMain,each))
>
> bgMainLayout.activate()
>
> But the items never showup in my buttongroup.
Neither you or the QButtonGroup are keeping a reference to the QPushButton
so it is getting garbage collected immediately. I need to check the Qt
source to see if the QButtonGroup should keep a reference (in which case
it's a PyQt bug) or you need to do it explicitly.
Phil
More information about the PyQt
mailing list