From a8c04721afd4457b4590db892bb246918dab1076 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Mon, 22 Jan 2024 09:37:41 +0100 Subject: [PATCH] Add virtual QObjectPrivate::writeToDebugStream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QObject by default writes class- and objectName to the debug stream, but QObject subclasses might want to write different, or additional information. Providing class-specific debug stream operators is not a good solution for this, as it doesn't respect runtime polymorphism. With a virtual helper in QObjectPrivate, class authors can add an override. Change-Id: I382126ae5a56391d0c7e5ccb58fbbafa6aab5c77 Reviewed-by: Tor Arne Vestbø --- src/corelib/kernel/qobject.cpp | 16 ++++++++++++---- src/corelib/kernel/qobject_p.h | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index d57e13c018..a7dd23048a 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -4472,15 +4472,23 @@ void QObject::dumpObjectInfo() const #ifndef QT_NO_DEBUG_STREAM +void QObjectPrivate::writeToDebugStream(QDebug &dbg) const +{ + Q_Q(const QObject); + dbg.nospace() << q->metaObject()->className() << '(' << (const void *)q; + if (!q->objectName().isEmpty()) + dbg << ", name = " << q->objectName(); + dbg << ')'; +} + QDebug operator<<(QDebug dbg, const QObject *o) { QDebugStateSaver saver(dbg); if (!o) return dbg << "QObject(0x0)"; - dbg.nospace() << o->metaObject()->className() << '(' << (const void *)o; - if (!o->objectName().isEmpty()) - dbg << ", name = " << o->objectName(); - dbg << ')'; + + const QObjectPrivate *d = QObjectPrivate::get(o); + d->writeToDebugStream(dbg); return dbg; } #endif diff --git a/src/corelib/kernel/qobject_p.h b/src/corelib/kernel/qobject_p.h index f42b523c67..68ca9f53a2 100644 --- a/src/corelib/kernel/qobject_p.h +++ b/src/corelib/kernel/qobject_p.h @@ -188,6 +188,10 @@ public: virtual std::string flagsForDumping() const; +#ifndef QT_NO_DEBUG_STREAM + virtual void writeToDebugStream(QDebug &) const; +#endif + QtPrivate::QPropertyAdaptorSlotObject * getPropertyAdaptorSlotObject(const QMetaProperty &property);