QSignalMapper: deprecate overridden signals

Task-number: QTBUG-80906
Change-Id: I9beab78d8fe8dfc712969f65792f50360a890287
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Vitaly Fanaskov 2020-01-30 14:35:45 +01:00
parent e56b8e1e59
commit dd42e5d5e7
4 changed files with 112 additions and 32 deletions

View File

@ -61,14 +61,13 @@ ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, &QPushButton::clicked,
signalMapper, &QSignalMapper::map);
connect(button, &QPushButton::clicked, signalMapper, &QSignalMapper::map);
//! [0] //! [1]
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}
connect(signalMapper, QOverload<const QString &>::of(&QSignalMapper::mapped),
connect(signalMapper, &QSignalMapper::mappedString,
//! [1] //! [2]
this, &ButtonWidget::clicked);

View File

@ -53,11 +53,36 @@ public:
Q_Q(QSignalMapper);
q->removeMappings(q->sender());
}
template <class Signal, class Container>
void emitMappedValue(QObject *sender, Signal signal, const Container &mappedValues)
{
Q_Q(QSignalMapper);
auto it = mappedValues.find(sender);
if (it != mappedValues.end()) {
#if QT_DEPRECATED_SINCE(5, 15)
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
Q_EMIT q->mapped(*it);
QT_WARNING_POP
#endif
Q_EMIT (q->*signal)(*it);
}
}
void emitMappedValues(QObject *sender)
{
emitMappedValue(sender, &QSignalMapper::mappedInt, intHash);
emitMappedValue(sender, &QSignalMapper::mappedString, stringHash);
emitMappedValue(sender, &QSignalMapper::mappedWidget, widgetHash);
emitMappedValue(sender, &QSignalMapper::mappedObject, objectHash);
}
QHash<QObject *, int> intHash;
QHash<QObject *, QString> stringHash;
QHash<QObject *, QWidget*> widgetHash;
QHash<QObject *, QObject*> objectHash;
};
/*!
@ -74,11 +99,12 @@ public:
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 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
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(),
mappedWidget() 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
@ -106,8 +132,8 @@ public:
created. We connect each button's \c 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 \c
clicked() signal. When the user clicks a button, the custom
the signal mapper's mappedString() signal to the custom widget's
\c clicked() signal. When the user clicks a button, the custom
widget will emit a single \c clicked() signal whose argument is
the text of the button the user clicked.
@ -137,7 +163,7 @@ QSignalMapper::~QSignalMapper()
/*!
Adds a mapping so that when map() is signalled from the given \a
sender, the signal mapped(\a id) is emitted.
sender, the signal mappedInt(\a id) is emitted.
There may be at most one integer ID for each sender.
@ -152,7 +178,7 @@ void QSignalMapper::setMapping(QObject *sender, int id)
/*!
Adds a mapping so that when map() is signalled from the \a sender,
the signal mapped(\a text ) is emitted.
the signal mappedString(\a text ) is emitted.
There may be at most one text for each sender.
*/
@ -165,7 +191,7 @@ void QSignalMapper::setMapping(QObject *sender, const QString &text)
/*!
Adds a mapping so that when map() is signalled from the \a sender,
the signal mapped(\a widget ) is emitted.
the signal mappedWidget(\a widget ) is emitted.
There may be at most one widget for each sender.
*/
@ -178,7 +204,7 @@ void QSignalMapper::setMapping(QObject *sender, QWidget *widget)
/*!
Adds a mapping so that when map() is signalled from the \a sender,
the signal mapped(\a object ) is emitted.
the signal mappedObject(\a object ) is emitted.
There may be at most one object for each sender.
*/
@ -259,20 +285,14 @@ void QSignalMapper::map() { map(sender()); }
*/
void QSignalMapper::map(QObject *sender)
{
Q_D(QSignalMapper);
if (d->intHash.contains(sender))
emit mapped(d->intHash.value(sender));
if (d->stringHash.contains(sender))
emit mapped(d->stringHash.value(sender));
if (d->widgetHash.contains(sender))
emit mapped(d->widgetHash.value(sender));
if (d->objectHash.contains(sender))
emit mapped(d->objectHash.value(sender));
d_func()->emitMappedValues(sender);
}
#if QT_DEPRECATED_SINCE(5, 15)
/*!
\fn void QSignalMapper::mapped(int i)
\obsolete
\overload
This signal is emitted when map() is signalled from an object that
has an integer mapping set. The object's mapped integer is passed
@ -283,6 +303,8 @@ void QSignalMapper::map(QObject *sender)
/*!
\fn void QSignalMapper::mapped(const QString &text)
\obsolete
\overload
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
@ -293,6 +315,8 @@ void QSignalMapper::map(QObject *sender)
/*!
\fn void QSignalMapper::mapped(QWidget *widget)
\obsolete
\overload
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
@ -303,6 +327,53 @@ void QSignalMapper::map(QObject *sender)
/*!
\fn void QSignalMapper::mapped(QObject *object)
\obsolete
\overload
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
\a object.
\sa setMapping()
*/
#endif
/*!
\fn void QSignalMapper::mappedInt(int i)
\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 \a i.
\sa setMapping()
*/
/*!
\fn void QSignalMapper::mappedString(const QString &text)
\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
\a text.
\sa setMapping()
*/
/*!
\fn void QSignalMapper::mappedWidget(QWidget *widget)
\since 5.15
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
\a widget.
\sa setMapping()
*/
/*!
\fn void QSignalMapper::mappedObject(QObject *object)
\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

View File

@ -66,10 +66,20 @@ public:
QObject *mapping(QObject *object) const;
Q_SIGNALS:
#if QT_DEPRECATED_SINCE(5, 15)
QT_DEPRECATED_VERSION_X_5_15("Use QSignalMapper::mappedInt(int) instead")
void mapped(int);
QT_DEPRECATED_VERSION_X_5_15("Use QSignalMapper::mappedString(const QString&) instead")
void mapped(const QString &);
QT_DEPRECATED_VERSION_X_5_15("Use QSignalMapper::mappedWidget(QWidget *) instead")
void mapped(QWidget *);
QT_DEPRECATED_VERSION_X_5_15("Use QSignalMapper::mappedObject(QObject *) instead")
void mapped(QObject *);
#endif
void mappedInt(int);
void mappedString(const QString &);
void mappedWidget(QWidget *);
void mappedObject(QObject *);
public Q_SLOTS:
void map();

View File

@ -41,8 +41,8 @@ class QtTestObject : public QObject
{
Q_OBJECT
public slots:
void myslot(int id);
void myslot(const QString &str);
void myIntSlot(int id);
void myStringSlot(const QString &str);
signals:
void mysignal(int);
@ -54,12 +54,12 @@ public:
QString str;
};
void QtTestObject::myslot(int id)
void QtTestObject::myIntSlot(int id)
{
this->id = id;
}
void QtTestObject::myslot(const QString &str)
void QtTestObject::myStringSlot(const QString &str)
{
this->str = str;
}
@ -71,7 +71,7 @@ void QtTestObject::emit_mysignal(int value)
void tst_QSignalMapper::mapped()
{
QSignalMapper mapper(0);
QSignalMapper mapper;
QtTestObject target;
QtTestObject src1;
@ -88,8 +88,8 @@ void tst_QSignalMapper::mapped()
mapper.setMapping(&src2, "two");
mapper.setMapping(&src3, "three");
connect(&mapper, SIGNAL(mapped(int)), &target, SLOT(myslot(int)));
connect(&mapper, SIGNAL(mapped(QString)), &target, SLOT(myslot(QString)));
connect(&mapper, &QSignalMapper::mappedInt, &target, &QtTestObject::myIntSlot);
connect(&mapper, &QSignalMapper::mappedString, &target, &QtTestObject::myStringSlot);
src1.emit_mysignal(20);
QCOMPARE(target.id, 1);