Add virtual QObjectPrivate::writeToDebugStream

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ø <tor.arne.vestbo@qt.io>
bb10
Volker Hilsheimer 2024-01-22 09:37:41 +01:00
parent 2c51a0bb7b
commit a8c04721af
2 changed files with 16 additions and 4 deletions

View File

@ -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

View File

@ -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);