From 0e8279b7bcad4c639562bb034a20866d9b2e8192 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 4 Jul 2012 17:18:22 +0200 Subject: [PATCH] QObject: add a macro for conveniently setting the object name This is a simplified port of KDTools' KDAB_SET_OBJECT_NAME. It simply assigns the variable name as the objectName of a QObject, uic-style. It uses a small helper function so that it works on references as well as pointer variables. QLabel label; QLabel *pLabel = new QLabel(); Q_SET_OBJECT_NAME(label); Q_SET_OBJECT_NAME(pLabel); Change-Id: I25fec0c90f33249a3ea5d2dd622ab708019fd101 Reviewed-by: Sean Harmer Reviewed-by: David Faure Reviewed-by: Stephen Kelly Reviewed-by: Lars Knoll --- src/corelib/kernel/qobject.cpp | 13 +++++++++++++ src/corelib/kernel/qobject.h | 6 ++++++ tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 16 ++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index a3d5c1e..9f8b766 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -4023,6 +4023,19 @@ QDebug operator<<(QDebug dbg, const QObject *o) { */ /*! + \macro Q_SET_OBJECT_NAME(Object) + \relates QObject + \since 5.0 + + This macro assigns \a Object the objectName "Object". + + It doesn't matter whether \a Object is a pointer or not, the + macro figures that out by itself. + + \sa QObject::objectName() +*/ + +/*! \typedef QObjectList \relates QObject diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 9cb5ab9..1d2d6a6 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -524,6 +524,12 @@ template inline const char * qobject_interface_iid() Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *); #endif +namespace QtPrivate { + inline QObject & deref_for_methodcall(QObject &o) { return o; } + inline QObject & deref_for_methodcall(QObject *o) { return *o; } +} +#define Q_SET_OBJECT_NAME(obj) QT_PREPEND_NAMESPACE(QtPrivate)::deref_for_methodcall(obj).setObjectName(QLatin1String(#obj)) + QT_END_NAMESPACE QT_END_HEADER diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 479cdd3..9adc191 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -563,14 +563,14 @@ void tst_QObject::findChildren() QTimer t121(&o12); QTimer emptyname(&o); - o.setObjectName("o"); - o1.setObjectName("o1"); - o2.setObjectName("o2"); - o11.setObjectName("o11"); - o12.setObjectName("o12"); - o111.setObjectName("o111"); - t1.setObjectName("t1"); - t121.setObjectName("t121"); + Q_SET_OBJECT_NAME(o); + Q_SET_OBJECT_NAME(o1); + Q_SET_OBJECT_NAME(o2); + Q_SET_OBJECT_NAME(o11); + Q_SET_OBJECT_NAME(o12); + Q_SET_OBJECT_NAME(o111); + Q_SET_OBJECT_NAME(t1); + Q_SET_OBJECT_NAME(t121); emptyname.setObjectName(""); QObject *op = 0; -- 2.7.4