summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qobject.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2024-03-25 10:41:25 -0400
committerThiago Macieira <thiago.macieira@intel.com>2024-04-03 14:15:43 -0700
commitce2585d0e950ff0d81adbcf5463ddfbcb1367900 (patch)
tree2466402ded6b6dc37e1cae8cb98852b5a03c37a7 /src/corelib/kernel/qobject.h
parent43cefd882e15f1b3b99d25a17db2057411ff465c (diff)
QObject: add check for Q_OBJECT macro to findChild(ren)
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 <giuseppe.dangelo@kdab.com> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/kernel/qobject.h')
-rw-r--r--src/corelib/kernel/qobject.h6
1 files changed, 6 insertions, 0 deletions
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<typename std::remove_pointer<T>::type>::type ObjType;
+ static_assert(QtPrivate::HasQ_OBJECT_Macro<ObjType>::Value,
+ "No Q_OBJECT in the class passed to QObject::findChild");
return static_cast<T>(qt_qFindChild_helper(this, aName, ObjType::staticMetaObject, options));
}
@@ -162,6 +164,8 @@ public:
QList<T> findChildren(QAnyStringView aName, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
{
typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type ObjType;
+ static_assert(QtPrivate::HasQ_OBJECT_Macro<ObjType>::Value,
+ "No Q_OBJECT in the class passed to QObject::findChildren");
QList<T> list;
qt_qFindChildren_helper(this, aName, ObjType::staticMetaObject,
reinterpret_cast<QList<void *> *>(&list), options);
@@ -185,6 +189,8 @@ public:
inline QList<T> findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
{
typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type ObjType;
+ static_assert(QtPrivate::HasQ_OBJECT_Macro<ObjType>::Value,
+ "No Q_OBJECT in the class passed to QObject::findChildren");
QList<T> list;
qt_qFindChildren_helper(this, re, ObjType::staticMetaObject,
reinterpret_cast<QList<void *> *>(&list), options);