forked from qt-creator/qt-creator
Coding rules: Add a note about avoiding sender() and QSignalMapper
Change-Id: Ifbc73b3c1e18484c692ee0e2a2999f2fe2e98f8b Reviewed-by: Eike Ziller <eike.ziller@qt.io> Reviewed-by: hjk <hjk@qt.io> Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
This commit is contained in:
@@ -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<QAction *>(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
|
||||
|
Reference in New Issue
Block a user