Python Bindings for KDE (PyKDE-3.16.0)
Prev |  | Next

# Signal and Slot Support

## General Signals and Slots

A signal may be either a TQt signal (specified using `TQ_SIGNAL()`) or a Python signal (specified using `PYSIGNAL()`).

A slot can be either a Python callable object, a TQt signal (specified using `TQ_SIGNAL()`), a Python signal (specified using `PYSIGNAL()`), or a TQt slot (specified using `TQ_SLOT()`).

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

    TQObject.connect(a,TQ_SIGNAL("TQtSig()"),pyFunction)
    TQObject.connect(a,TQ_SIGNAL("TQtSig()"),pyClass.pyMethod)
    TQObject.connect(a,TQ_SIGNAL("TQtSig()"),PYSIGNAL("PySig"))
    TQObject.connect(a,TQ_SIGNAL("TQtSig()"),TQ_SLOT("TQtSlot()"))
    TQObject.connect(a,PYSIGNAL("PySig"),pyFunction)
    TQObject.connect(a,PYSIGNAL("PySig"),pyClass.pyMethod)
    TQObject.connect(a,PYSIGNAL("PySig"),TQ_SIGNAL("TQtSig()"))
    TQObject.connect(a,PYSIGNAL("PySig"),TQ_SLOT("TQtSlot()"))

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

    sbar = TQScrollBar()
    lcd = TQLCDNumber()

    TQObject.connect(sbar,TQ_SIGNAL("valueChanged(int)"),lcd.display)
    TQObject.connect(sbar,TQ_SIGNAL("valueChanged(int)"),lcd,TQ_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 `TQObject` class can emit a signal using the `emit` method. This takes two arguments. The first is the Python or TQt signal, the second is a Python tuple which are the arguments to the signal. For example:

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

TQt 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.

## Slots in Menus, Toolbars and Actions

The C++ declarations for menu items or TDEActions are similar to these examples:

    int TQMenuData::insertItem (const TQString & text,
         const TQObject * receiver, const char * member,
         int accel = 0, int id = -1, int index = -1 )

    TDEAction ( const TQString& text, int accel,
         const TQObject* receiver, const char* slot,
         TQObject* parent, const char* name = 0 )

Notice the "const TQObject* receiver, const char* slot" parameters for each declaration.

In PyKDE, these two parameters are replaced with a SINGLE parameter that specifies the slot to be connected to the menu item, toolbar button or TDEAction:

    p = insertItem ("Open", self.slotOpen, 0, -1, -1)

    action = TDEAction ("Open", 0, self.slotOpen, None, 0)

This substitution applies to appropriate methods in KStdAction, TDEAction and related subclasses, TDEAccelMenu and TDEToolBar

Prev | Home | Next
General Limitations |  | Static Member Functions