Table Of Contents

Previous topic

QSocketNotifier

Next topic

QMimeData

QSignalMapper

Inheritance diagram of QSignalMapper

Synopsis

Functions

Slots

Signals

Detailed Description

The PySide.QtCore.QSignalMapper class bundles signals from identifiable senders.

This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.

The class supports the mapping of particular strings or integers with particular objects using PySide.QtCore.QSignalMapper.setMapping() . The objects’ signals can then be connected to the PySide.QtCore.QSignalMapper.map() slot which will emit the PySide.QtCore.QSignalMapper.mapped() signal with the string or integer associated with the original signalling object. Mappings can be removed later using PySide.QtCore.QSignalMapper.removeMappings() .

Example: Suppose we want to create a custom widget that contains a group of buttons (like a tool palette). One approach is to connect each button’s clicked() signal to its own custom slot; but in this example we want to connect all the buttons to a single slot and parameterize the slot by the button that was clicked.

Here’s the definition of a simple custom widget that has a single signal, clicked() , which is emitted with the text of the button that was clicked:

class ButtonWidget(QWidget):
    def __init__(self, texts, parent=None):
        QWidget.__init__(self, parent)
        ...

The only function that we need to implement is the constructor:

def __init__(self, texts, parent):
    QWidget.__init__(self, parent)

    self.signalMapper = QSignalMapper(self)

    layout = QGridLayout()

    for text, index in enumerate(texts):
        button = QPushButton(text)
        self.connect(SIGNAL("clicked()"), self.signalMapper, SLOT("map()"))
        self.signalMapper.setMapping(button, text)
        layout.addWidget(button, index / 3, index % 3)

    self.signalMapper.connect(SIGNAL("mapped(const QString &)"),
            self, SLOT("clicked(const QString &)"))

    self.setLayout(layout)

A list of texts is passed to the constructor. A signal mapper is constructed and for each text in the list a PySide.QtGui.QPushButton is created. We connect each button’s clicked() signal to the signal mapper’s PySide.QtCore.QSignalMapper.map() slot, and create a mapping in the signal mapper from each button to the button’s text. Finally we connect the signal mapper’s PySide.QtCore.QSignalMapper.mapped() signal to the custom widget’s clicked() signal. When the user clicks a button, the custom widget will emit a single clicked() signal whose argument is the text of the button the user clicked.

class PySide.QtCore.QSignalMapper([parent=None])
Parameters:parentPySide.QtCore.QObject

Constructs a PySide.QtCore.QSignalMapper with parent parent .

PySide.QtCore.QSignalMapper.map(sender)
Parameters:senderPySide.QtCore.QObject

This slot emits signals based on the sender object.

PySide.QtCore.QSignalMapper.map()

This slot emits signals based on which object sends signals to it.

PySide.QtCore.QSignalMapper.mapped(arg__1)
Parameters:arg__1PySide.QtCore.int
PySide.QtCore.QSignalMapper.mapped(arg__1)
Parameters:arg__1 – unicode
PySide.QtCore.QSignalMapper.mapped(arg__1)
Parameters:arg__1PySide.QtGui.QWidget
PySide.QtCore.QSignalMapper.mapped(arg__1)
Parameters:arg__1PySide.QtCore.QObject
PySide.QtCore.QSignalMapper.mapping(id)
Parameters:idPySide.QtCore.int
Return type:PySide.QtCore.QObject

Returns the sender PySide.QtCore.QObject that is associated with the id .

PySide.QtCore.QSignalMapper.mapping(text)
Parameters:text – unicode
Return type:PySide.QtCore.QObject

This function overloads PySide.QtCore.QSignalMapper.mapping() .

PySide.QtCore.QSignalMapper.mapping(widget)
Parameters:widgetPySide.QtGui.QWidget
Return type:PySide.QtCore.QObject

This function overloads PySide.QtCore.QSignalMapper.mapping() .

Returns the sender PySide.QtCore.QObject that is associated with the widget .

PySide.QtCore.QSignalMapper.mapping(object)
Parameters:objectPySide.QtCore.QObject
Return type:PySide.QtCore.QObject

This function overloads PySide.QtCore.QSignalMapper.mapping() .

Returns the sender PySide.QtCore.QObject that is associated with the object .

PySide.QtCore.QSignalMapper.removeMappings(sender)
Parameters:senderPySide.QtCore.QObject

Removes all mappings for sender .

This is done automatically when mapped objects are destroyed.

PySide.QtCore.QSignalMapper.setMapping(sender, text)
Parameters:

Adds a mapping so that when PySide.QtCore.QSignalMapper.map() is signalled from the sender , the signal mapped(text ) is emitted.

There may be at most one text for each sender.

PySide.QtCore.QSignalMapper.setMapping(sender, widget)
Parameters:

Adds a mapping so that when PySide.QtCore.QSignalMapper.map() is signalled from the sender , the signal mapped(widget ) is emitted.

There may be at most one widget for each sender.

PySide.QtCore.QSignalMapper.setMapping(sender, object)
Parameters:

Adds a mapping so that when PySide.QtCore.QSignalMapper.map() is signalled from the sender , the signal mapped(object ) is emitted.

There may be at most one object for each sender.

PySide.QtCore.QSignalMapper.setMapping(sender, id)
Parameters:

Adds a mapping so that when PySide.QtCore.QSignalMapper.map() is signalled from the given sender , the signal mapped(id ) is emitted.

There may be at most one integer ID for each sender.