Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QSignalMapper Class Reference

\inmodule QtCore More...

#include <qsignalmapper.h>

+ Inheritance diagram for QSignalMapper:
+ Collaboration diagram for QSignalMapper:

Public Slots

void map ()
 This slot emits signals based on which object sends signals to it.
 
void map (QObject *sender)
 This slot emits signals based on the sender object.
 
- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 

Signals

void mappedInt (int)
 
void mappedString (const QString &)
 
void mappedObject (QObject *)
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QSignalMapper (QObject *parent=nullptr)
 Constructs a QSignalMapper with parent parent.
 
 ~QSignalMapper ()
 Destroys the QSignalMapper.
 
void setMapping (QObject *sender, int id)
 Adds a mapping so that when map() is signalled from the given sender, the signal mappedInt(id) is emitted.
 
void setMapping (QObject *sender, const QString &text)
 Adds a mapping so that when map() is signalled from the sender, the signal mappedString(text ) is emitted.
 
void setMapping (QObject *sender, QObject *object)
 Adds a mapping so that when map() is signalled from the sender, the signal mappedObject(object ) is emitted.
 
void removeMappings (QObject *sender)
 Removes all mappings for sender.
 
QObjectmapping (int id) const
 Returns the sender QObject that is associated with the id.
 
QObjectmapping (const QString &text) const
 
QObjectmapping (QObject *object) const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Detailed Description

\inmodule QtCore

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. Note that in most cases you can use lambdas for passing custom parameters to slots. This is less costly and will simplify the code.

The class supports the mapping of particular strings, integers, objects and widgets with particular objects using setMapping(). The objects' signals can then be connected to the map() slot which will emit a signal (it could be mappedInt(), mappedString() and mappedObject()) with a value 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:

class ButtonWidget : public QWidget
{
public:
ButtonWidget(const QStringList &texts, QWidget *parent = nullptr);
void clicked(const QString &text);
private:
QSignalMapper *signalMapper;

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

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

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 mappedString() 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.

This class was mostly useful before lambda functions could be used as slots. The example above can be rewritten simpler without QSignalMapper by connecting to a lambda function.

: QWidget(parent)
{
for (int i = 0; i < texts.size(); ++i) {
QString text = texts[i];
connect(button, &QPushButton::clicked, [this, text] { clicked(text); });
gridLayout->addWidget(button, i / 3, i % 3);
}
}
void mappedString(const QString &)
void map()
This slot emits signals based on which object sends signals to it.
See also
QObject, QButtonGroup, QActionGroup

Definition at line 13 of file qsignalmapper.h.

Constructor & Destructor Documentation

◆ QSignalMapper()

QSignalMapper::QSignalMapper ( QObject * parent = nullptr)
explicit

Constructs a QSignalMapper with parent parent.

Definition at line 103 of file qsignalmapper.cpp.

◆ ~QSignalMapper()

QSignalMapper::~QSignalMapper ( )

Destroys the QSignalMapper.

Definition at line 111 of file qsignalmapper.cpp.

Member Function Documentation

◆ map [1/2]

void QSignalMapper::map ( )
slot

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

Definition at line 207 of file qsignalmapper.cpp.

References map(), and QObject::sender().

Referenced by ButtonWidget::ButtonWidget(), and map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ map [2/2]

void QSignalMapper::map ( QObject * sender)
slot

This slot emits signals based on the sender object.

Definition at line 212 of file qsignalmapper.cpp.

References QObject::sender().

+ Here is the call graph for this function:

◆ mappedInt

void QSignalMapper::mappedInt ( int i)
signal
Since
5.15

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 QSignalMapperPrivate::emitMappedValues().

+ Here is the caller graph for this function:

◆ mappedObject

void QSignalMapper::mappedObject ( QObject * object)
signal
Since
5.15

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()

Referenced by QSignalMapperPrivate::emitMappedValues().

+ Here is the caller graph for this function:

◆ mappedString

void QSignalMapper::mappedString ( const QString & text)
signal
Since
5.15

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()

Referenced by ButtonWidget::ButtonWidget(), and QSignalMapperPrivate::emitMappedValues().

+ Here is the caller graph for this function:

◆ mapping() [1/3]

QObject * QSignalMapper::mapping ( const QString & text) const

Definition at line 170 of file qsignalmapper.cpp.

References d.

◆ mapping() [2/3]

QObject * QSignalMapper::mapping ( int id) const

Returns the sender QObject that is associated with the id.

See also
setMapping()

Definition at line 161 of file qsignalmapper.cpp.

References d.

◆ mapping() [3/3]

QObject * QSignalMapper::mapping ( QObject * object) const

Definition at line 181 of file qsignalmapper.cpp.

References d.

◆ removeMappings()

void QSignalMapper::removeMappings ( QObject * sender)

Removes all mappings for sender.

This is done automatically when mapped objects are destroyed.

Note
This does not disconnect any signals. If sender is not destroyed then this will need to be done explicitly if required.

Definition at line 195 of file qsignalmapper.cpp.

References d, and QObject::sender().

Referenced by setMapping(), setMapping(), and setMapping().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setMapping() [1/3]

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

Adds a mapping so that when map() is signalled from the sender, the signal mappedString(text ) is emitted.

There may be at most one text for each sender.

Definition at line 136 of file qsignalmapper.cpp.

References QObject::connect(), d, QObject::destroyed(), removeMappings(), QObject::sender(), and text.

+ Here is the call graph for this function:

◆ setMapping() [2/3]

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

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

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

See also
mapping()

Definition at line 123 of file qsignalmapper.cpp.

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

Referenced by ButtonWidget::ButtonWidget().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setMapping() [3/3]

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

Adds a mapping so that when map() is signalled from the sender, the signal mappedObject(object ) is emitted.

There may be at most one object for each sender.

Definition at line 149 of file qsignalmapper.cpp.

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

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: