Optimize code sample of QObject::isSignalConnected

Since isSignalConnected is meant to be used in performance critical
code, and that QMetaMethod::fromSignal is relatively slow, it is better
to have it a a static variable in the code sample, as a good
recommendation on how to use it.

Since QMetaMethod::fromSignal should not change during the life of the
program, it should be safe.  Also, if different threads run at the same
time, both should lead to the same result.

Change-Id: Ib6113d11ca93f216bc3a92aea4eaa4da6a4151ca
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Olivier Goffart 2013-02-01 14:39:23 +01:00 committed by The Qt Project
parent 3ba61d9baa
commit c5c584c116

View File

@ -480,7 +480,8 @@ QObject::disconnect(lineEdit, &QLineEdit::textChanged,
//! [48] //! [48]
//! [49] //! [49]
if (isSignalConnected(QMetaMethod::fromSignal(&MyObject::valueChanged))) { static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
if (isSignalConnected(valueChangedSignal)) {
QByteArray data; QByteArray data;
data = get_the_value(); // expensive operation data = get_the_value(); // expensive operation
emit valueChanged(data); emit valueChanged(data);