CopperSpice API  1.7.2
QSignalMapper Class Reference

The QSignalMapper class bundles signals from identifiable senders. More...

Inheritance diagram for QSignalMapper:
QObject

Public Signals

void mapped (const QString &text)
 
void mapped (int index)
 
void mapped (QObject *object)
 
void mapped (QWidget *widget)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void map ()
 
void map (QObject *sender)
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QSignalMapper (QObject *parent=nullptr)
 
 ~QSignalMapper ()
 
QObjectmapping (const QString &text) const
 
QObjectmapping (int id) const
 
QObjectmapping (QObject *object) const
 
QObjectmapping (QWidget *widget) const
 
void removeMappings (QObject *sender)
 
void setMapping (QObject *sender, const QString &text)
 
void setMapping (QObject *sender, int id)
 
void setMapping (QObject *sender, QObject *object)
 
void setMapping (QObject *sender, QWidget *widget)
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Additional Inherited Members

- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Properties inherited from QObject
 objectName
 

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 signaling 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 is 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 : public QWidget {
CS_OBJECT(ButtonWidget)
public:
ButtonWidget(QStringList texts, QWidget *parent = nullptr);
CS_SIGNAL_1(Public, void clicked(const QString &text))
CS_SIGNAL_2(clicked, text)
private:
QSignalMapper *signalMapper;
};

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

ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent) : QWidget(parent)
{
signalMapper = new QSignalMapper(this);
QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}
connect(signalMapper, SIGNAL(mapped(const QString &)), this, SIGNAL(clicked(const QString &)));
setLayout(gridLayout);
}

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

Constructor & Destructor Documentation

QSignalMapper::QSignalMapper ( QObject parent = nullptr)
explicit

Constructs a QSignalMapper with parent parent.

QSignalMapper::~QSignalMapper ( )

Destroys the QSignalMapper.

Method Documentation

void QSignalMapper::map ( )
slot

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

void QSignalMapper::map ( QObject sender)
slot

This slot emits signals based on the sender object.

void QSignalMapper::mapped ( const QString text)
signal

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 ( int  index)
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 index.

See also
setMapping()
void QSignalMapper::mapped ( QObject object)
signal

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::mapped ( QWidget widget)
signal

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()
QObject * QSignalMapper::mapping ( const QString text) const

Returns the sender QObject associated with the given text.

QObject * QSignalMapper::mapping ( int  id) const

Returns the sender QObject associated with the given id.

See also
setMapping()
QObject * QSignalMapper::mapping ( QObject object) const

Returns the sender QObject that is associated with the object.

QObject * QSignalMapper::mapping ( QWidget widget) const

Returns the sender QObject that is associated with the widget.

void QSignalMapper::removeMappings ( QObject sender)

Removes all mappings for sender. This is done automatically when mapped objects are destroyed.

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

Adds a mapping so when map() is triggered from the sender, the signal mapped(text) is emitted. There may be at most one text for each sender.

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

Adds a mapping so when map() is triggered from the given sender, the signal mapped(id) is emitted. There may be at most one integer ID for each sender.

See also
mapping()
void QSignalMapper::setMapping ( QObject sender,
QObject object 
)

Adds a mapping so when map() is triggered from the sender, the signal mapped(object) is emitted. There may be at most one object for each sender.

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

Adds a mapping so when map() is triggered from the sender, the signal mapped(widget) is emitted. There may be at most one widget for each sender.