QSignalBlocker Class

Exception-safe wrapper around QObject::blockSignals(). More...

Header: #include <QSignalBlocker>
qmake: QT += core
Since: Qt 5.3

Note: All functions in this class are reentrant.

Public Functions

Detailed Description

Exception-safe wrapper around QObject::blockSignals().

QSignalBlocker can be used wherever you would otherwise use a pair of calls to blockSignals(). It blocks signals in its constructor and in the destructor it resets the state to what it was before the constructor ran.

 {
 const QSignalBlocker blocker(someQObject);
 // no signals here
 }

is thus equivalent to

 const bool wasBlocked = someQObject->blockSignals(true);
 // no signals here
 someQObject->blockSignals(wasBlocked);

except the code using QSignalBlocker is safe in the face of exceptions.

See also QMutexLocker and QEventLoopLocker.

Member Function Documentation

QSignalBlocker::~QSignalBlocker()

Destructor. Restores the QObject::signalsBlocked() state to what it was before the constructor ran, unless unblock() has been called without a following reblock(), in which case it does nothing.