From f61753c495a58779f423c8a80c0ad4ee5fa6ea0f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 14 Mar 2022 15:13:53 +0100 Subject: QObject: give some TLC to dumpRecursive() In no particular order: - use the variable field width feature of QString::asprintf() to generate the indentation implicitly, instead of fill()ing a QByteArray with the desired number of spaces - just default-construct 'flags', don't assign an empty string - use qUtf16Printable() to avoid funneling UTF-16 data through 8-bit encodings - use a C++11 ranged for instead of a counted loop - remove a pointless isEmpty() guard (the loop won't execute when the children().isEmpty()) - avoid copying object->children() (returns by cref, so it's also ok to stuff it directly into the ranged for loop). Add a test. Change-Id: Ie7314713cb48de7e890cdee0760c0361dd24fd18 Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira (cherry picked from commit e74e27e67eab8b714ddc2f3f7fc2175fd6dcbe5a) Reviewed-by: Qt Cherry-pick Bot --- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index bf30282822..af4b35fb46 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -108,6 +108,7 @@ private slots: void deleteSelfInSlot(); void disconnectSelfInSlotAndDeleteAfterEmit(); void dumpObjectInfo(); + void dumpObjectTree(); void connectToSender(); void qobjectConstCast(); void uniqConnection(); @@ -3413,6 +3414,32 @@ void tst_QObject::dumpObjectInfo() a.dumpObjectInfo(); // should not crash } +void tst_QObject::dumpObjectTree() +{ + QObject a; + Q_SET_OBJECT_NAME(a); + + QTimer b(&a); + Q_SET_OBJECT_NAME(b); + + QObject c(&b); + Q_SET_OBJECT_NAME(c); + + QFile f(&a); + Q_SET_OBJECT_NAME(f); + + const char * const output[] = { + "QObject::a ", + " QTimer::b ", + " QObject::c ", + " QFile::f ", + }; + for (const char *line : output) + QTest::ignoreMessage(QtDebugMsg, line); + + a.dumpObjectTree(); +} + class ConnectToSender : public QObject { Q_OBJECT public slots: -- cgit v1.2.3