[PyKDE] QPushButton

Gordon Tyler gordon at doxxx.net
Tue Sep 2 19:33:01 BST 2003


GuineaPig wrote:
> I'm working with pyQt and I have a widget with 40 QPushButtons 
> (toggles).  On 'toggled()' the buttons emit a signal to a function.  In 
> this function I'd like to find out wich button was pressed.  I'm 
> thinking about iterating through all the buttons to check theirs state 
> but I can't seem to find how to do this in the docs.  Can someone point 
> me in the right direction ?  Should I handle this differently ?

You might want to try QSignalMapper: 
http://doc.trolltech.com/3.2/qsignalmapper.html

Code looks something like this (untested):

def initGUI(self):
     sigmap = QSignalMapper(self)

     pushButton1 = QPushButton("Button 1", self)
     self.connect(pushButton1, SIGNAL("toggled(bool)"), sigmap, 
SLOT("map()")
     sigmap.setMapping(pushButton1, 1)

     pushButton2 = QPushButton("Button 2", self)
     self.connect(pushButton2, SIGNAL("toggled(bool)"), sigmap, 
SLOT("map()")
     sigmap.setMapping(pushButton2, 2)

     self.connect(sigmap, SIGNAL("mapped(int)"), self.buttonPushed)

def buttonPushed(self, index):
     print "Button #%d pushed" % index

Unfortunately, it does look like the toggled signal's "bool on" 
parameter can't be carried through but since you'll know which 
QPushButton it is, you can easily check it's state.

Ciao,
Gordon





More information about the PyQt mailing list