[PyKDE] callback not being called
    David Boddie 
    david at boddie.org.uk
       
    Sun Jan 21 18:36:48 GMT 2007
    
    
  
On Sunday 21 January 2007 09:05:40 -0800, Tony Cappellini wrote:
> I've trying to understand how to make a cmd button call a method when the
> button is pressed.
>
> I havne't been seen an example in the Signals & Slots section of the QT
> docs which  describe how to connect a widget signal to a callback function,
> only how to call a widget slot function.
Do you mean that you want to call normal methods in Python classes and not
just those supplied by their Qt base classes?
> I don't get any runtime errors in my attempt to do this, but the callback
> function isn't being called.
>
> What is the preferred method of registering a non-widget slot to be called
> by a widget signal?
You should be able to use the same syntax as that used in the Qt
documentation, but there's also a PyQt-specific shortcut for slots that you
can use.
[Re-using your code]
class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.patchBrowser = QtGui.QTextEdit()
        # layout code omitted for brevity
    def updateTextBox(self):
        # this function should be called, when the button is pressed
        self.patchBrowser.setPlainText(self.tr("Callback was called"))
    def createPatchnameComboBox(self):
        self.patchNames = QtGui.QComboBox()
        self.patchNames.addItem("Entry 1")
        self.patchNames.addItem("Entry 2")
        # layout code omitted for brevity
        # doesn't call updateTextBox
        #self.connect(self.patchNames, QtCore.SIGNAL("higlighted"),
        #             self, QtCore.SLOT("updateTextBox") )
        self.connect(self.patchNames, QtCore.SIGNAL("activated"),
                     self, QtCore.SLOT("updateTextBox") )
You need to pass the C++ signatures for signals and slots that are defined by
Qt, so you should SIGNAL("activated()") instead of SIGNAL("activated").
However, your updateTextBox() slot is just a normal Python method, so you can
just pass a reference to it:
        self.connect(self.patchNames, QtCore.SIGNAL("activated()"),
                     self.updateTextBox)
For slots defined by the C++ base class, you can use SLOT() to specify them
in the connect() call; for example, the following connection would cause
the dialog to be hidden when a patchNames combobox entry is activated:
        self.connect(self.patchNames, QtCore.SIGNAL("activated()"),
                     self, QtCore.SLOT("hide()"))
It's quite common that people forget to provide the full signature (with C++
types, but without argument names) to the SIGNAL() and SLOT() functions. It
would be interesting to know whether it seems like a strange convention to
people who have never used Qt with C++, or whether it catches everyone out
at some point.
David
    
    
More information about the PyQt
mailing list