Signal and Slot Support

A signal may be either a Qt signal (specified using SIGNAL()) or a Python signal (specified using PYSIGNAL()).

A slot can be either a Python callable object, a Qt signal (specified using SIGNAL()), a Python signal (specified using PYSIGNAL()), or a Qt slot (specified using SLOT()).

You connect signals to slots (and other signals) as you would from C++. For example:

QObject.connect(a,SIGNAL("QtSig()"),pyFunction)
QObject.connect(a,SIGNAL("QtSig()"),pyClass.pyMethod)
QObject.connect(a,SIGNAL("QtSig()"),PYSIGNAL("PySig"))
QObject.connect(a,SIGNAL("QtSig()"),SLOT("QtSlot()"))
QObject.connect(a,PYSIGNAL("PySig"),pyFunction)
QObject.connect(a,PYSIGNAL("PySig"),pyClass.pyMethod)
QObject.connect(a,PYSIGNAL("PySig"),SIGNAL("QtSig()"))
QObject.connect(a,PYSIGNAL("PySig"),SLOT("QtSlot()"))

When a slot is a Python method that corresponds to a Qt slot then a signal can be connected to either the Python method or the Qt slot. The following connections achieve the same effect.

sbar = QScrollBar()
lcd = QLCDNumber()

QObject.connect(sbar,SIGNAL("valueChanged(int)"),lcd.display)
QObject.connect(sbar,SIGNAL("valueChanged(int)"),lcd,SLOT("display(int)"))

The difference is that the second connection is made at the C++ level and is more efficient.

Disconnecting signals works in exactly the same way.

Any instance of a class that is derived from the QObject class can emit a signal using the emit method. This takes two arguments. The first is the Python or Qt signal, the second is a Python tuple which are the arguments to the signal. For example:

a.emit(SIGNAL("clicked()"),())
a.emit(PYSIGNAL("pySig"),("Hello","World"))

Note that when a slot is a Python callable object it's reference count is not increased. This means that a class instance can be deleted without having to explicitly disconnect any signals connected to its methods. However, it also means that using lambda expressions as slots will not work unless you keep a separate reference to the expression to prevent it from being immediately garbage collected.

Qt allows a signal to be connected to a slot that requires fewer arguments than the signal passes. The extra arguments are quietly discarded. Python slots can be used in the same way.