From ce2585d0e950ff0d81adbcf5463ddfbcb1367900 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. Pick-to: 6.7 Fixes: QTBUG-105023 Change-Id: I5f663c2f9f4149af84fefffd17c008f027241b56 Reviewed-by: Giuseppe D'Angelo Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Volker Hilsheimer --- 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 5145258029..06cfefd61b 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -155,6 +155,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)); } @@ -162,6 +164,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); @@ -185,6 +189,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);