From c08197ccab36c10287df2e509d0c7e0c87e29fa5 Mon Sep 17 00:00:00 2001 From: Lars Schmertmann Date: Thu, 12 Apr 2018 15:20:26 +0200 Subject: [PATCH] QObject: Extend debug output of connect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes it is hard to find the line, when the warning "QObject::connect: invalid null parameter" appears in the log. This change adds the class names of the sender and receiver to give a hint where to search for the wrong call to connect. Change-Id: I00cead7d943f96d60f198cb3f0bed34ba10285c5 Reviewed-by: André Hartmann --- src/corelib/kernel/qobject.cpp | 7 ++++++- .../corelib/kernel/qobject/tst_qobject.cpp | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index dcc1bb5814..25d17ecd6b 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -4802,7 +4802,12 @@ QMetaObject::Connection QObjectPrivate::connectImpl(const QObject *sender, int s const int *types, const QMetaObject *senderMetaObject) { if (!sender || !receiver || !slotObj || !senderMetaObject) { - qWarning("QObject::connect: invalid null parameter"); + const char *senderString = sender ? sender->metaObject()->className() + : senderMetaObject ? senderMetaObject->className() + : "Unknown"; + const char *receiverString = receiver ? receiver->metaObject()->className() + : "Unknown"; + qWarning("QObject::connect(%s, %s): invalid null parameter", senderString, receiverString); if (slotObj) slotObj->destroyIfLastRef(); return QMetaObject::Connection(); diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index c734cfe4dd..43dcb241e4 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -145,6 +145,7 @@ private slots: void disconnectDoesNotLeakFunctor(); void contextDoesNotLeakFunctor(); void connectBase(); + void connectWarnings(); void qmlConnect(); void exceptions(); void noDeclarativeParentChangedOnDestruction(); @@ -6685,6 +6686,26 @@ void tst_QObject::connectBase() QCOMPARE( r1.count_slot3, 1 ); } +void tst_QObject::connectWarnings() +{ + SubSender sub; + SenderObject obj; + ReceiverObject r1; + r1.reset(); + + QTest::ignoreMessage(QtWarningMsg, "QObject::connect(SenderObject, ReceiverObject): invalid null parameter"); + connect(nullptr, &SubSender::signal1, &r1, &ReceiverObject::slot1); + + QTest::ignoreMessage(QtWarningMsg, "QObject::connect(SubSender, Unknown): invalid null parameter"); + connect(&sub, &SubSender::signal1, nullptr, &ReceiverObject::slot1); + + QTest::ignoreMessage(QtWarningMsg, "QObject::connect(SenderObject, ReceiverObject): invalid null parameter"); + connect(nullptr, &SenderObject::signal1, &r1, &ReceiverObject::slot1); + + QTest::ignoreMessage(QtWarningMsg, "QObject::connect(SenderObject, Unknown): invalid null parameter"); + connect(&obj, &SenderObject::signal1, nullptr, &ReceiverObject::slot1); +} + struct QmlReceiver : public QtPrivate::QSlotObjectBase { int callCount;