QSignalMapper Class Reference

#include <qsignalmapper.h>

Inheritance diagram for QSignalMapper:

Inheritance graph
[legend]
Collaboration diagram for QSignalMapper:

Collaboration graph
[legend]
List of all members.

Detailed Description

The 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 setMapping(). The objects' signals can then be connected to the map() slot which will emit the mapped() signal with the string or integer associated with the original signalling object. Mappings can be removed later using 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:

snippets/qsignalmapper/buttonwidget.h QWidget QSignalMapper };

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

snippets/qsignalmapper/buttonwidget.cpp ButtonWidget connect connect }

A list of texts is passed to the constructor. A signal mapper is constructed and for each text in the list a QPushButton is created. We connect each button's clicked() signal to the signal mapper's 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 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.

See also:
QObject, QButtonGroup, QActionGroup

Definition at line 36 of file qsignalmapper.h.

Public Slots

void map ()
void map (QObject *sender)

Signals

void mapped (int)
void mapped (const QString &)
void mapped (QWidget *)
void mapped (QObject *)

Public Member Functions

 QSignalMapper (QObject *parent=0)
 ~QSignalMapper ()
void setMapping (QObject *sender, int id)
void setMapping (QObject *sender, const QString &text)
void setMapping (QObject *sender, QWidget *widget)
void setMapping (QObject *sender, QObject *object)
void removeMappings (QObject *sender)
QObjectmapping (int id) const
QObjectmapping (const QString &text) const
QObjectmapping (QWidget *widget) const
QObjectmapping (QObject *object) const


Constructor & Destructor Documentation

QSignalMapper::QSignalMapper ( QObject parent = 0  )  [explicit]

Constructs a QSignalMapper with parent parent.

Definition at line 103 of file qsignalmapper.cpp.

00104     : QObject(*new QSignalMapperPrivate, parent)
00105 {
00106 }

QSignalMapper::~QSignalMapper (  ) 

Destroys the QSignalMapper.

Definition at line 123 of file qsignalmapper.cpp.

00124 {
00125 }


Member Function Documentation

void QSignalMapper::setMapping ( QObject sender,
int  id 
)

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

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

See also:
mapping()

Definition at line 135 of file qsignalmapper.cpp.

References QObject::connect(), d, QObject::destroyed(), QObject::sender(), SIGNAL, and SLOT.

Referenced by addAction(), and MainWindow::updateWindowMenu().

00136 {
00137     Q_D(QSignalMapper);
00138     d->intHash.insert(sender, id);
00139     connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
00140 }

Here is the call graph for this function:

void QSignalMapper::setMapping ( QObject sender,
const QString text 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Adds a mapping so that when map() is signalled from the given sender, the signal mapped(text ) is emitted.

There may be at most one text for each sender.

Definition at line 150 of file qsignalmapper.cpp.

References QObject::connect(), d, QObject::destroyed(), QObject::sender(), SIGNAL, and SLOT.

00151 {
00152     Q_D(QSignalMapper);
00153     d->stringHash.insert(sender, text);
00154     connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
00155 }

Here is the call graph for this function:

void QSignalMapper::setMapping ( QObject sender,
QWidget widget 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Adds a mapping so that when map() is signalled from the given sender, the signal mapped(widget ) is emitted.

There may be at most one widget for each sender.

Definition at line 165 of file qsignalmapper.cpp.

References QObject::connect(), d, QObject::destroyed(), QObject::sender(), SIGNAL, and SLOT.

00166 {
00167     Q_D(QSignalMapper);
00168     d->widgetHash.insert(sender, widget);
00169     connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
00170 }

Here is the call graph for this function:

void QSignalMapper::setMapping ( QObject sender,
QObject object 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Adds a mapping so that when map() is signalled from the given sender, the signal mapped(object ) is emitted.

There may be at most one object for each sender.

Definition at line 180 of file qsignalmapper.cpp.

References QObject::connect(), d, QObject::destroyed(), object, QObject::sender(), SIGNAL, and SLOT.

00181 {
00182     Q_D(QSignalMapper);
00183     d->objectHash.insert(sender, object);
00184     connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
00185 }

Here is the call graph for this function:

void QSignalMapper::removeMappings ( QObject sender  ) 

Removes all mappings for sender.

This is done automatically when mapped objects are destroyed.

Definition at line 237 of file qsignalmapper.cpp.

References d, and QObject::sender().

00238 {
00239     Q_D(QSignalMapper);
00240 
00241     d->intHash.remove(sender);
00242     d->stringHash.remove(sender);
00243     d->widgetHash.remove(sender);
00244     d->objectHash.remove(sender);
00245 }

Here is the call graph for this function:

QObject * QSignalMapper::mapping ( int  id  )  const

Returns the sender QObject that is associated with the given id.

See also:
setMapping()

Definition at line 193 of file qsignalmapper.cpp.

References d.

00194 {
00195     Q_D(const QSignalMapper);
00196     return d->intHash.key(id);
00197 }

QObject * QSignalMapper::mapping ( const QString id  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 202 of file qsignalmapper.cpp.

References d.

00203 {
00204     Q_D(const QSignalMapper);
00205     return d->stringHash.key(id);
00206 }

QObject * QSignalMapper::mapping ( QWidget widget  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns the sender QObject that is associated with the given widget.

Definition at line 214 of file qsignalmapper.cpp.

References d.

00215 {
00216     Q_D(const QSignalMapper);
00217     return d->widgetHash.key(widget);
00218 }

QObject * QSignalMapper::mapping ( QObject object  )  const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns the sender QObject that is associated with the given object.

Definition at line 226 of file qsignalmapper.cpp.

References d, and object.

00227 {
00228     Q_D(const QSignalMapper);
00229     return d->objectHash.key(object);
00230 }

void QSignalMapper::mapped ( int  i  )  [signal]

This signal is emitted when map() is signalled from an object that has an integer mapping set. The object's mapped integer is passed in i.

See also:
setMapping()

Referenced by map().

void QSignalMapper::mapped ( const QString text  )  [signal]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This signal is emitted when map() is signalled from an object that has a string mapping set. The object's mapped string is passed in text.

See also:
setMapping()

void QSignalMapper::mapped ( QWidget widget  )  [signal]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This signal is emitted when map() is signalled from an object that has a widget mapping set. The object's mapped widget is passed in widget.

See also:
setMapping()

void QSignalMapper::mapped ( QObject object  )  [signal]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This signal is emitted when map() is signalled from an object that has an object mapping set. The object provided by the map is passed in object.

See also:
setMapping()

void QSignalMapper::map (  )  [slot]

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

Definition at line 250 of file qsignalmapper.cpp.

References QObject::sender().

00250 { map(sender()); }

void QSignalMapper::map ( QObject sender  )  [slot]

This slot emits signals based on the sender object.

Definition at line 255 of file qsignalmapper.cpp.

References d, emit, mapped(), and QObject::sender().

00256 {
00257     Q_D(QSignalMapper);
00258     if (d->intHash.contains(sender))
00259         emit mapped(d->intHash.value(sender));
00260     if (d->stringHash.contains(sender))
00261         emit mapped(d->stringHash.value(sender));
00262     if (d->widgetHash.contains(sender))
00263         emit mapped(d->widgetHash.value(sender));
00264     if (d->objectHash.contains(sender))
00265         emit mapped(d->objectHash.value(sender));
00266 }


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:54:44 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1