From 9c1752d7b19b67bd7cd2f8229b80a13aeb61becd Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 25 Mar 2024 10:41:25 -0400 Subject: [PATCH] QObject: add check for Q_OBJECT macro to findChild(ren) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can't fix the underlying reported problem, but we can warn that the user has made a mistake. [ChangeLog][QtCore][QObject] The class template parameter passed to QObject::findChild() or findChildren() is now required to have the Q_OBJECT macro. Forgetting to add it could result in finding children of the nearest ancestor class that has the macro. Fixes: QTBUG-105023 Change-Id: I5f663c2f9f4149af84fefffd17c008f027241b56 Reviewed-by: Giuseppe D'Angelo Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Volker Hilsheimer (cherry picked from commit ce2585d0e950ff0d81adbcf5463ddfbcb1367900) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qobject.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 76863d66bcf..6fe575f29ec 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -148,6 +148,8 @@ public: T findChild(QAnyStringView aName, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { typedef typename std::remove_cv::type>::type ObjType; + static_assert(QtPrivate::HasQ_OBJECT_Macro::Value, + "No Q_OBJECT in the class passed to QObject::findChild"); return static_cast(qt_qFindChild_helper(this, aName, ObjType::staticMetaObject, options)); } @@ -155,6 +157,8 @@ public: QList findChildren(QAnyStringView aName, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { typedef typename std::remove_cv::type>::type ObjType; + static_assert(QtPrivate::HasQ_OBJECT_Macro::Value, + "No Q_OBJECT in the class passed to QObject::findChildren"); QList list; qt_qFindChildren_helper(this, aName, ObjType::staticMetaObject, reinterpret_cast *>(&list), options); @@ -178,6 +182,8 @@ public: inline QList findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { typedef typename std::remove_cv::type>::type ObjType; + static_assert(QtPrivate::HasQ_OBJECT_Macro::Value, + "No Q_OBJECT in the class passed to QObject::findChildren"); QList list; qt_qFindChildren_helper(this, re, ObjType::staticMetaObject, reinterpret_cast *>(&list), options);