From 96085f327b2d1e89fb2896b902dd770379e692b3 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 14 Dec 2023 10:35:38 +0100 Subject: [PATCH] Coding rules: Add a note about avoiding sender() and QSignalMapper Change-Id: Ifbc73b3c1e18484c692ee0e2a2999f2fe2e98f8b Reviewed-by: Eike Ziller Reviewed-by: hjk Reviewed-by: Leena Miettinen --- doc/qtcreatordev/src/coding-style.qdoc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/qtcreatordev/src/coding-style.qdoc b/doc/qtcreatordev/src/coding-style.qdoc index 042cd58bb42..49e99a8e8bc 100644 --- a/doc/qtcreatordev/src/coding-style.qdoc +++ b/doc/qtcreatordev/src/coding-style.qdoc @@ -1003,6 +1003,32 @@ signal and slot lookup a few cycles faster. You can use $QTDIR/util/normalize to normalize existing code. For more information, see QMetaObject::normalizedSignature. + \li Avoid using \c{QObject::sender()}. Pass the sender explicitly instead. + \code + connect(action, &QAction::triggered, this, [this, action] { + onActionTriggered(action); + }); + ... + void MyClass::onActionTriggered(QAction *action) + { + ... use action ... + } + + -NOT- + + connect(action, &QAction::triggered, this, &MyClass::onActionTriggered); + ... + void MyClass::onActionTriggered() + { + QAction *action = qobject_cast(sender()); + if (action == nullptr) + return; + + ... use action ... + } + \endcode + \li Avoid \c{QSignalMapper}. Use a lambda instead and pass the mapped value + via the lambda capture. \endlist \section2 File Headers