[PyKDE] Emitting a signal from Python

David Boddie david at boddie.org.uk
Mon Feb 5 23:41:25 GMT 2007


On Sat, 3 Feb 2007 22:45:57 -0800, Tony Cappellini wrote:

> I've tried to add a line of code to emit a signal.
>
> The sender is normally a combo box, the receiver is a Text Edit box.
> The connect statement (that works) looks like this
>
> self.connect(self.Names, QtCore.SIGNAL("activated(const QString &)"),
> self.updateNameBrowser)

Looks good.

> I want to 'simulate' the user selecting an item in the combo box,
> however I can't quite get the Python syntax correct.
>
> I want to use this a.emit(QtCore.SIGNAL("clicked()"))
>
> taken from here
> http://www.riverbankcomputing.com/Docs/PyQt4/pyqt4ref.html#emitting-signals
>
> but I don't know what object to subsitute 'a' for

The clicked() signal is usually provided by various buttons, so the above
example isn't exactly what you want.

> When I try using the combo box
>
> self.Names.emit(QtCore.SIGNAL("activated()"))
>
> the desired result doesn't occur. That is, the TextEdit never receives
> the signal.

You need to use exactly the same signal name - in this case you connected
the combo box's signal to a Python slot with

  self.connect(self.Names, QtCore.SIGNAL("activated(const QString &)"),
               self.updateNameBrowser)

so you want to use emit the activated(const QString &) signal. Since this
signal has a parameter (const QString &), you need to pass an argument to
the emit() method:

  self.Names.emit(QtCore.SIGNAL("activated(const QString &)"), text)

where text is some string value.

Although you can get away without using SLOT() in Python, and thereby avoid
dealing with C++ type signatures, it's easy to forget that you have to be
explicit when dealing with SIGNAL().

David




More information about the PyQt mailing list