summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2012-03-01 18:52:32 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-30 12:56:44 +0100
commited827acc27530a97b84685920615359010d74f48 (patch)
treef603df3e597531a5bb6a6a21d4a565ba8262d614 /tests/auto/corelib/kernel/qobject/tst_qobject.cpp
parentbf6a345baa21818010242566bef4f2c25cb72437 (diff)
QSignalBlocker: (new) RAII class for QObject::blockSignals()
I don't think I ever worked on a project of non-trivial size that didn't at some point add a QSignalBlocker. This commit adds code, tests and documentation. Later commits will convert naked blockSignals() calls to use QSignalBlocker. The implementation is purely inline to avoid the heavy overhead of cross-dll function calls for this miniscule task. This should not be a problem because QSignalBlocker only uses public API and a pattern that we anyway need to keep working until Qt 6, at least, so even changing the implementation later will be no problem as the old implementation lurking in non-recompiled code will be acceptable, too. This implementation is an evolution from KDTools' KDSignalBlocker, with the following changes: - Implements unblock() and reblock() - Uses the return value of blockSignals() instead of a separate signalsBlocked() call. Change-Id: I1933dfd72a0f5190324be377cfca3c54cf3d6828 Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto/corelib/kernel/qobject/tst_qobject.cpp')
-rw-r--r--tests/auto/corelib/kernel/qobject/tst_qobject.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
index 05d81c2bd1..f429500077 100644
--- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
+++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
@@ -102,6 +102,7 @@ private slots:
#ifndef QT_NO_PROCESS
void recursiveSignalEmission();
#endif
+ void signalBlocking();
void blockingQueuedConnection();
void childEvents();
void installEventFilter();
@@ -2979,6 +2980,54 @@ void tst_QObject::recursiveSignalEmission()
}
#endif
+void tst_QObject::signalBlocking()
+{
+ SenderObject sender;
+ ReceiverObject receiver;
+
+ receiver.connect(&sender, SIGNAL(signal1()), SLOT(slot1()));
+
+ sender.emitSignal1();
+ QVERIFY(receiver.called(1));
+ receiver.reset();
+
+ {
+ QSignalBlocker blocker(&sender);
+
+ sender.emitSignal1();
+ QVERIFY(!receiver.called(1));
+ receiver.reset();
+
+ sender.blockSignals(false);
+
+ sender.emitSignal1();
+ QVERIFY(receiver.called(1));
+ receiver.reset();
+
+ sender.blockSignals(true);
+
+ sender.emitSignal1();
+ QVERIFY(!receiver.called(1));
+ receiver.reset();
+
+ blocker.unblock();
+
+ sender.emitSignal1();
+ QVERIFY(receiver.called(1));
+ receiver.reset();
+
+ blocker.reblock();
+
+ sender.emitSignal1();
+ QVERIFY(!receiver.called(1));
+ receiver.reset();
+ }
+
+ sender.emitSignal1();
+ QVERIFY(receiver.called(1));
+ receiver.reset();
+}
+
void tst_QObject::blockingQueuedConnection()
{
{