From 7406949858a282a029913d6fba5684dff5e51185 Mon Sep 17 00:00:00 2001 From: Andreas Hartmetz Date: Sun, 30 Aug 2020 12:01:15 +0200 Subject: Doc fix: disconnect with receiver also works for context objects One could guess it by assuming that disconnecting for a destroyed receiver and disconnect() with given receiver use the same implementation, but without closely knowing the implementation a reader of the documentation can't know for sure. Also add a test to prove that what the new documentation says is really true. Also remove an unnecessary negation in the preceding sentence. Change-Id: I9d24442bb1a4646b89f969bad1a4d0e1eafa7534 Reviewed-by: Mitch Curtis Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qobject.cpp | 5 ++-- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 35 ++++++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 62bee3d90b..b749e3916c 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -5009,8 +5009,9 @@ bool QObject::disconnect(const QMetaObject::Connection &connection) any signal. If not, only the specified signal is disconnected. If \a receiver is \nullptr, it disconnects anything connected to \a - signal. If not, slots in objects other than \a receiver are not - disconnected. + signal. If not, only slots in the specified receiver are disconnected. + disconnect() with a non-null \a receiver also disconnects slot functions + that were connected with \a receiver as their context object. If \a method is \nullptr, it disconnects anything that is connected to \a receiver. If not, only slots named \a method will be disconnected, diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index c84c812823..1d274d8c95 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -6054,13 +6054,41 @@ void tst_QObject::connectFunctorWithContext() connect(context, &QObject::destroyed, &obj, &SenderObject::signal1, Qt::QueuedConnection); context->deleteLater(); - QCOMPARE(status, 1); + obj.emitSignal1(); + QCOMPARE(status, 2); e.exec(); - QCOMPARE(status, 1); + QCOMPARE(status, 2); + + // Check disconnect with the context object as "receiver" argument, all signals + context = new ContextObject; + status = 1; + connect(&obj, &SenderObject::signal1, context, SlotArgFunctor(&status)); + + obj.emitSignal1(); + QCOMPARE(status, 2); + QObject::disconnect(&obj, nullptr, context, nullptr); + obj.emitSignal1(); + QCOMPARE(status, 2); + + delete context; + + // Check disconnect with the context object as "receiver" argument, specific signal + context = new ContextObject; + status = 1; + connect(&obj, &SenderObject::signal1, context, SlotArgFunctor(&status)); + + obj.emitSignal1(); + QCOMPARE(status, 2); + QObject::disconnect(&obj, &SenderObject::signal1, context, nullptr); + obj.emitSignal1(); + QCOMPARE(status, 2); + + delete context; // Check the sender arg is set correctly in the context context = new ContextObject; + status = 1; connect(&obj, &SenderObject::signal1, context, SlotArgFunctor(context, &obj, &status), Qt::QueuedConnection); @@ -6077,8 +6105,7 @@ void tst_QObject::connectFunctorWithContext() e.exec(); QCOMPARE(status, 2); - // Free - context->deleteLater(); + delete context; } class StatusChanger : public QObject -- cgit v1.2.3