summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Hartmetz <andreas@ixgreen.de>2020-08-30 12:01:15 +0200
committerAndreas Hartmetz <andreas@ixgreen.de>2020-09-01 13:35:39 +0200
commit7406949858a282a029913d6fba5684dff5e51185 (patch)
treec781409ba3eee3b105e38b892aad42dfc4faaf86
parent7451453154690cb3e7e3ed6eccc1da8b555650b9 (diff)
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 <mitch.curtis@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/corelib/kernel/qobject.cpp5
-rw-r--r--tests/auto/corelib/kernel/qobject/tst_qobject.cpp35
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