6.0.0-rc4Source

QSignalMapper Class

The QSignalMapper class bundles signals from identifiable senders. More…

Header#include <QSignalMapper>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQObject

List of all members, including inherited members

Public Functions

QSignalMapper(QObject *parent = nullptr)
~QSignalMapper()
voidsetMapping(QObject *sender, int id)
voidsetMapping(QObject *sender, const QString &text)
voidsetMapping(QObject *sender, QObject *object)
voidremoveMappings(QObject *sender)
QObject *mapping(int id) const
QObject *mapping(const QString &text) const
QObject *mapping(QObject *object) const

Public Slots

voidmap()
voidmap(QObject *sender)

Signals

voidmappedInt(int i)
voidmappedString(const QString &text)
voidmappedObject(QObject *object)

Detailed Description

A mapper collects the signals of several objects and re-emits each as a signal that carries an identifier of the sender: an integer, a string or an object. It lets one slot serve many senders, which is handy when the senders are created in a loop:

QSignalMapper mapper;
for (int i = 0; i < 3; ++i) {
    QTimer *timer = new QTimer(&mapper);
    mapper.setMapping(timer, i);
    QObject::connect(timer, &QTimer::timeout, &mapper, qOverload<>(&QSignalMapper::map));
}
QObject::connect(&mapper, &QSignalMapper::mappedInt, [](int i) { qDebug() << "timer" << i; });

Connect each sender's signal to map(), which finds the sender with QObject::sender(), or to the map() overload that takes it explicitly. A sender can have an integer, a string and an object mapping at the same time; one signal is emitted for each. A mapping is dropped when its sender is destroyed.

See also QObject::sender().

Differences from Qt

The Qt 5 signals mapped(int), mapped(const QString &) and mapped(QObject *) were removed in Qt 6, and are not provided here either; there is no mappedWidget().

Member Function Documentation

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

Constructs a mapper with the given parent.

[override] QSignalMapper::~QSignalMapper()

Destroys the mapper.

[slot] void QSignalMapper::map()

Emits the signals that correspond to the mappings of the sender of the signal that called this slot, as found by QObject::sender(). Does nothing if the sender has no mapping.

See also setMapping().

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

This is an overloaded function.

Emits the signals that correspond to the mappings of sender, in the order mappedInt(), mappedString(), mappedObject().

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

This signal is emitted when map() is called by a sender that has an integer mapping, with the identifier i of that mapping.

[signal] void QSignalMapper::mappedObject(QObject *object)

This signal is emitted when map() is called by a sender that has an object mapping, with the object of that mapping.

[signal] void QSignalMapper::mappedString(const QString &text)

This signal is emitted when map() is called by a sender that has a string mapping, with the text of that mapping.

QObject * QSignalMapper::mapping(int id) const

Returns the sender that has the integer mapping id, or nullptr if there is none.

See also setMapping().

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

This is an overloaded function.

Returns the sender that has the string mapping text, or nullptr if there is none.

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

This is an overloaded function.

Returns the sender that has the object mapping object, or nullptr if there is none.

void QSignalMapper::removeMappings(QObject *sender)

Removes all mappings of sender.

See also setMapping().

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

Adds a mapping so that when map() is called by sender, mappedInt() is emitted with id. Replaces an earlier integer mapping of sender.

See also mapping() and removeMappings().

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

This is an overloaded function.

Adds a mapping so that when map() is called by sender, mappedString() is emitted with text.

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

This is an overloaded function.

Adds a mapping so that when map() is called by sender, mappedObject() is emitted with object.