diff options
| author | Ahmad Samir <a.samirh78@gmail.com> | 2025-09-12 20:41:17 +0300 |
|---|---|---|
| committer | Ahmad Samir <a.samirh78@gmail.com> | 2025-10-02 01:40:20 +0300 |
| commit | 24ccf8c5a0b6232594fe22c8524f3db016e55b1d (patch) | |
| tree | e4835e15b2a67b9cba1c0ff8480b94a98aca33b9 /src/corelib/kernel/qobject.cpp | |
| parent | 15b1bd2c819996321dd8e76f5e7be5a9dc670f58 (diff) | |
QObject: warn about using SLOT macro with non-slot functions
And make it no-op in Qt7.
[ChangeLog][QtCore][Important Behavior Change] A warning will be shown
if the SLOT() macro is used with a function not marked as a slot, but
it'll continue to work to keep backwards-compatibility. However in Qt7
the SLOT() macro will be no-op for non-slot functions.
Change-Id: I9ea112b7a72a26fcc5548ebc3c0220eab19d0455
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qobject.cpp')
| -rw-r--r-- | src/corelib/kernel/qobject.cpp | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 85bba774af6..a7afc08a345 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -2669,6 +2669,22 @@ static bool check_method_code(int code, const QObject *object, const char *metho return true; } +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) +static void check_and_warn_non_slot(const char *func, const char *method, int membcode, + const QMetaObject *rmeta, const QMetaMethod &rmethod) +{ + if (membcode == QSLOT_CODE && rmethod.methodType() != QMetaMethod::Slot) { + // In Qt7 QMetaObject::indexOfSlot{,relative} will return -1 if `method` + // isn't a slot. + qCWarning(lcConnect, + "QObject::%s: the SLOT() macro is used with a non-slot function: %s::%s. " + "This currently works due to backwards-compatibility reasons. In Qt7 the " + "SLOT() macro will work only for methods marked as slots.", + func, rmeta->className(), method); + } +} +#endif // QT_VERSION < QT_VERSION_CHECK(7, 0, 0) + Q_DECL_COLD_FUNCTION static void err_method_notfound(const QObject *object, const char *method, const char *func) @@ -3143,11 +3159,16 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign return QMetaObject::Connection(nullptr); } + QMetaMethod rmethod = rmeta->method(method_index_relative + rmeta->methodOffset()); #ifndef QT_NO_DEBUG QMetaMethod smethod = QMetaObjectPrivate::signal(smeta, signal_index); - QMetaMethod rmethod = rmeta->method(method_index_relative + rmeta->methodOffset()); check_and_warn_compat(smeta, smethod, rmeta, rmethod); #endif + +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) + check_and_warn_non_slot("connect", method, membcode, rmeta, rmethod); +#endif + QMetaObject::Connection handle = QMetaObject::Connection(QMetaObjectPrivate::connect( sender, signal_index, smeta, receiver, method_index_relative, rmeta ,type, types)); return handle; @@ -3420,9 +3441,14 @@ bool QObject::disconnect(const QObject *sender, const char *signal, // Already computed the method_index for receiver->metaObject() above if (rmeta != receiver->metaObject()) method_index = getMethodIndex(membcode, rmeta, methodName, methodTypes); - if (method_index >= 0) + if (method_index >= 0) { +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) + check_and_warn_non_slot("disconnect", method, membcode, rmeta, + rmeta->method(method_index)); +#endif while (method_index < rmeta->methodOffset()) rmeta = rmeta->superClass(); + } if (method_index < 0) break; res |= QMetaObjectPrivate::disconnect(sender, signal_index, smeta, receiver, method_index, nullptr); |
