QSignalMapper Class
The QSignalMapper class bundles signals from identifiable senders. More…
| Header | #include <QSignalMapper> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QSignalMapper(QObject *parent = nullptr) | |
| ~QSignalMapper() | |
| void | setMapping(QObject *sender, int id) |
| void | setMapping(QObject *sender, const QString &text) |
| void | setMapping(QObject *sender, QObject *object) |
| void | removeMappings(QObject *sender) |
| QObject * | mapping(int id) const |
| QObject * | mapping(const QString &text) const |
| QObject * | mapping(QObject *object) const |
Public Slots
Signals
| void | mappedInt(int i) |
| void | mappedString(const QString &text) |
| void | mappedObject(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
QSignalMapper::QSignalMapper(QObject *parent = nullptr)
Constructs a mapper with the given parent.
QSignalMapper::~QSignalMapper()
Destroys the mapper.
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().
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().
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.
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.
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.