[PyQt] Problem connecting buttonClicked signal

Darryl Wallace darryl.wallace at prosensus.ca
Tue Jun 22 21:50:56 BST 2010


Hello
> -----Original Message-----
> From: pyqt-bounces at riverbankcomputing.com [mailto:pyqt-
> bounces at riverbankcomputing.com] On Behalf Of dizou
> Sent: June-22-10 2:11 PM
> To: pyqt at riverbankcomputing.com
> Subject: [PyQt] Problem connecting buttonClicked signal
>
>
> I have a bunch of QPushButtons in a QButtonGroup. I am trying to connect
the
> buttonClicked() signal with a method, but I am not able to.
>
> This is what I have:
>
> class DrawWidget(QWidget):
>     def __init__(self, parent):
>         QWidget.__init__(self, parent)
>
>         self.Setup()
>     def Setup(self):
>         cursorButton = QPushButton()
>         platformButton = QPushButton()
>         nodeButton = QPushButton()
>         channelButton = QPushButton()
>
>         buttonGroup = QButtonGroup()
>         buttonGroup.setExclusive(True)
>         buttonGroup.addButton(cursorButton, 0)
>         buttonGroup.addButton(platformButton, 1)
>         buttonGroup.addButton(nodeButton, 2)
>         buttonGroup.addButton(channelButton, 3)
>
>         for button in buttonGroup.buttons():
>             button.setFlat(True)
>             button.setCheckable(True)
>
>         cursorButton.setChecked(True)
>         gridLayout = QGridLayout()
>         gridLayout.addWidget(cursorButton, 0, 0)
>         gridLayout.addWidget(platformButton, 0, 1)
>         gridLayout.addWidget(nodeButton, 0, 2)
>         gridLayout.addWidget(channelButton, 0, 3)
>
>         self.setLayout(gridLayout)
>
>         self.connect(buttonGroup, SIGNAL("buttonClicked(int)"),
> self.ButtonClick)
>         #self.connect(buttonGroup, SIGNAL("clicked(int)"),
self.ButtonClick)
>     def ButtonClick(self, id):
>         print id
>         print "clicked"
>
> When I run this code, I don't get anything printed on the screen.

The reason you don't get anything is because "buttonGroup" is being
garbage collected because it goes out of scope after "Setup" is completed.
You have to set it to "self.buttonGroup" so that something holds reference
to it.

Darryl


More information about the PyQt mailing list